traits

Can the stackable trait pattern be used with singleton objects?

爷,独闯天下 提交于 2019-12-01 03:54:05
I'd like to use the stackable trait pattern with singleton objects, but i can't seem to find how to make the compiler happy: abstract class Pr { def pr() } trait PrePostPr extends Pr { abstract override def pr() { println("prepr") super.pr() println("postpr") } } object Foo extends Pr with PrePostPr { def pr() = println("Foo") } Trying to evaluate this in the repl produces the following error: <console>:10: error: overriding method pr in trait PrePostPr of type ()Unit; method pr needs `override' modifier def pr() = println("Foo") It can, but like this: abstract class Pr { def pr() } trait

Extending a Scala collection

对着背影说爱祢 提交于 2019-12-01 03:54:02
问题 I want a Map that throws on attempt to overwrite a value for existing key. I tried: trait Unoverwriteable[A, B] extends scala.collection.Map[A, B] { case class KeyAlreadyExistsException(e: String) extends Exception(e) abstract override def + [B1 >: B] (kv: (A, B1)): Unoverwriteable[A, B1] = { if (this contains(kv _1)) throw new KeyAlreadyExistsException( "key already exists in WritableOnce map: %s".format((kv _1) toString) ) super.+(kv) } abstract override def get(key: A): Option[B] = super

Compiler forces me to implement trait method but the `Self` trait bound on method is never satisfied for my type

懵懂的女人 提交于 2019-12-01 03:40:35
I have a trait Foo . I want to force implementors to define a method, if those implementors implement another trait ( Clone in this example). My idea ( Playground ): trait Foo { // Note: in my real application, the trait has other methods as well, // so I can't simply add `Clone` as super trait fn foo(&self) where Self: Clone; } struct NoClone; impl Foo for NoClone {} Sadly, this leads to: error[E0046]: not all trait items implemented, missing: `foo` --> src/lib.rs:8:1 | 2 | / fn foo(&self) 3 | | where 4 | | Self: Clone; | |____________________- `foo` from trait ... 8 | impl Foo for NoClone {}

Traits - property conflict with parent class

依然范特西╮ 提交于 2019-12-01 03:05:31
I have this class Zgh\FEBundle\Entity\User which extends FOS\UserBundle\Model\User . use FOS\UserBundle\Model\User as BaseUser; class User extends BaseUser implements ParticipantInterface { use BasicInfo; // .. } And BaseUser class: abstract class User implements UserInterface, GroupableInterface { protected $id; // .. } And BaseInfo trait: trait BasicInfo { /** * @ORM\Column(type="string", length=255) * @ORM\Id * @ORM\GeneratedValue(strategy="NONE") */ protected $id; // .. } But when I run my code i get this error: Strict standards: FOS\UserBundle\Model\User and Zgh\FEBundle\Model\Partial

value reduceByKey is not a member of org.apache.spark.rdd.RDD

烂漫一生 提交于 2019-12-01 02:37:19
问题 It's very sad.My spark version is 2.1.1,Scala version is 2.11 import org.apache.spark.SparkContext._ import com.mufu.wcsa.component.dimension.{DimensionKey, KeyTrait} import com.mufu.wcsa.log.LogRecord import org.apache.spark.rdd.RDD object PV { // def stat[C <: LogRecord,K <:DimensionKey](statTrait: KeyTrait[C ,K],logRecords: RDD[C]): RDD[(K,Int)] = { val t = logRecords.map(record =>(statTrait.getKey(record),1)).reduceByKey((x,y) => x + y) I got this error at 1502387780429 [ERROR] /Users

In scala multiple inheritance, how to resolve conflicting methods with same signature but different return type?

拜拜、爱过 提交于 2019-12-01 02:29:52
Consider the code below: trait A { def work = { "x" } } trait B { def work = { 1 } } class C extends A with B { override def work = super[A].work } Class C won't compile in scala 2.10, because of "overriding method work in trait A of type => String; method work has incompatible type". How to choose one specific method? I'm afraid there is no way to do that. The super[A].work way works only if A and B have the same return types. Consider this: class D extends B .... val test: List[B] = List(new C(), new D()) test.map(b => b.work) //oops - C returns a String, D returns an Int Scala simply

Scala trait - Is there an equivalent of Java interface public static field?

一世执手 提交于 2019-12-01 02:16:39
In Java: public interface Foo { public static final int Bar = 0; } And in Scala, how can I create a trait Foo that has Bar , and I can access it as: Foo.Bar ? You can create a companion object (to make it the equivalent of static) and define the variable there using the final val keywords (to make it the equivalent of a final constant): trait Foo { } object Foo { final val Bar = 0 } Lots more on this here 来源: https://stackoverflow.com/questions/8867316/scala-trait-is-there-an-equivalent-of-java-interface-public-static-field

Rust invoke trait method on generic type parameter

浪子不回头ぞ 提交于 2019-12-01 01:18:45
问题 Suppose I have a rust trait that contains a function that does not take a &self parameter. Is there a way for me to call this function based on a generic type parameter of the concrete type that implements that trait? For example, in the get_type_id function below, how do I successfully call the type_id() function for the CustomType trait? pub trait TypeTrait { fn type_id() -> u16; } pub struct CustomType { // fields... } impl TypeTrait for CustomType { fn type_id() -> u16 { 0 } } pub fn get

How to use a trait several times in a class?

梦想与她 提交于 2019-12-01 01:02:03
问题 The following code: trait T { function foo() {} } class C { use T { T::foo as bar; } use T { T::foo as baz; } } Produces the following error: Trait method bar has not been applied, because there are collisions with other trait methods on C Is it possible to use a trait twice in a class? 回答1: To "import" a method defined in a trait multiple times with different names do this: class C { use T { foo as bar; foo as baz; } } 回答2: Yes, you can use a trait twice: trait T { function foo() {} } class

Can the stackable trait pattern be used with singleton objects?

拥有回忆 提交于 2019-12-01 00:58:01
问题 I'd like to use the stackable trait pattern with singleton objects, but i can't seem to find how to make the compiler happy: abstract class Pr { def pr() } trait PrePostPr extends Pr { abstract override def pr() { println("prepr") super.pr() println("postpr") } } object Foo extends Pr with PrePostPr { def pr() = println("Foo") } Trying to evaluate this in the repl produces the following error: <console>:10: error: overriding method pr in trait PrePostPr of type ()Unit; method pr needs