traits

Traits and abstract methods override in Scala

我的未来我决定 提交于 2019-12-03 02:00:02
问题 I have a base abstract class (trait). It has an abstract method foo() . It is extended and implemented by several derived classes. I want to create a trait that can be mixed into the derived classes so that it implements foo() and then calls the derived class's foo() . Something like: trait Foo { def foo() } trait M extends Foo { override def foo() { println("M") super.foo() } } class FooImpl1 extends Foo { override def foo() { println("Impl") } } class FooImpl2 extends FooImpl1 with M I

Unexpected Trait Behavior

非 Y 不嫁゛ 提交于 2019-12-02 20:37:06
Given a simple Algebraic Data Type of Parent : scala> sealed trait Parent defined trait Parent scala> case object Boy extends Parent defined object Boy scala> case object Girl extends Parent defined object Girl I defined a trait: scala> trait HasGirl { | val x: Girl.type | } defined trait HasGirl Then, I created a case class that implemented HasGirl , but provided an x value of Boy.type . scala> case class Thing(x: Boy.type) extends HasGirl defined class Thing I had expected a compile-time error, since I don't see how an x of type Boy.type conforms to val x: Girl.type . What's going on here?

How do I best share behavior among Akka actors?

本小妞迷上赌 提交于 2019-12-02 20:32:49
I have two Akka actors that respond to some messages in the same way, but others in a different way. They both respond to the same set of messages. Wondering how to design my two actors with their receive methods, via inheritance, composure, etc? I tried chaining together partial functions from other traits with "orElse", which unfortunately exposes the class to its trait's functionality, plus I wasn't sure how the trait's receive could easily access the actor context. A drop-in, modularized solution would be ideal, but I'm wondering if this is a solved problem somewhere? There's really a

Is there a way to extend trait in PHP?

ぃ、小莉子 提交于 2019-12-02 19:59:44
I want to use functionality of existing trait and create my own trait on top of it, only to later apply it on classes. Precisely I want to extend Laravel SoftDeletes trait to make SaveWithHistory function, so it will create a copy of a current state of a record as deleted record. I also want to extend it with record_made_by_user_id field. Filip Koblański Yes, there is. You just have to define new trait like this: trait MySoftDeletes { use SoftDeletes { SoftDeletes::saveWithHistory as parentSaveWithHistory; } public function saveWithHistory() { $this->parentSaveWithHistory(); //your

Implement IntoIterator for binary tree

旧街凉风 提交于 2019-12-02 19:37:57
问题 I am trying to build a binary tree and write an iterator to traverse values in the tree. When implementing the IntoIterator trait for my tree nodes I ran into a problem with lifetimes src\main.rs:43:6: 43:8 error: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates [E0207] src\main.rs:43 impl<'a, T: 'a> IntoIterator for Node<T> { I understand that I need to specify that NodeIterator will live as long as Node but I am unsure of how to express that use std

Can I simulate traits/mixins in Swift?

不羁岁月 提交于 2019-12-02 18:59:40
Does Swift have a way of mixing in traits, a la Scala? The section of the Swift book on using extensions to add protocols to existing classes comes tantalizingly close. However, since protocols can't contain an implementation, this can't be used to mix code into a class. Is there another way? As of Swift 2.0, yes! Providing Default Implementations You can use protocol extensions to provide a default implementation to any method or property requirement of that protocol. If a conforming type provides its own implementation of a required method or property, that implementation will be used

Using scala constructor to set variable defined in trait

[亡魂溺海] 提交于 2019-12-02 17:52:27
If I understand correctly, traits are the closest thing to Java interfaces and class constructors automatically set the variables. But what if I have a class that extends a trait and has a constructor which sets a variable from the trait, so something like: trait Foo { var foo: String } class Bar (foo: String) extends Foo { /* ... */ } Where I want the foo string of the trait been set when I make a Bar object. The compiler seems to give me errors about this. What is the correct way to achieve this? Bar must define the abstract var foo in Foo (would be the same for a val ). This can be done in

Why PHP Trait can't implement interfaces?

谁都会走 提交于 2019-12-02 17:13:42
I'm wondering why PHP Trait (PHP 5.4) cannot implement interfaces. Update from user1460043's answer => ...cannot require class which uses it to implement a specific interface I understand that it could be obvious, because people could think that if a Class A is using a Trait T which is implementing an interface I , than the Class A should be implementing the interface I undirectly (and this is not true because Class A could rename trait methods). In my case, my trait is calling methods from the interface that the class using the trait implements. The trait is in fact an implementation of some

Is it possible to have a generic function on a trait?

给你一囗甜甜゛ 提交于 2019-12-02 16:08:15
问题 I have: struct Plumbus<'a> { grumbo: &'a Grumbo, } trait Grumbo { fn dinglebop<T>(&self, x: &mut T) -> bool { false } } but I get: error[E0038]: the trait `Grumbo` cannot be made into an object --> plumbus.rs:4:5 | 4 | grumbo: &'a Grumbo, | ^^^^^^^^^^^^^^^^^^ the trait `Grumbo` cannot be made into an object | = note: method `dinglebop` has generic type parameters I want to have dinglebop do nothing by default, but depending on the Grumbo and the T , possibly fill x with a T if it makes sense

What are the pros of using traits over abstract classes?

大兔子大兔子 提交于 2019-12-02 15:00:52
Can someone please explain traits in Scala? What are the advantages of traits over extending an abstract class? The short answer is that you can use multiple traits -- they are "stackable". Also, traits cannot have constructor parameters. Here's how traits are stacked. Notice that the ordering of the traits are important. They will call each other from right to left. class Ball { def properties(): List[String] = List() override def toString() = "It's a" + properties.mkString(" ", ", ", " ") + "ball" } trait Red extends Ball { override def properties() = super.properties ::: List("red") } trait