traits

How are Scala's traits not really traits?

一曲冷凌霜 提交于 2019-12-03 06:49:26
Someone recently told me that Scala's traits aren't "true" traits, and that they were really just mixins. Unfortunately, I didn't get the opportunity to ask him why. Does anyone have an idea what he meant? Edit: As a definition of "traits," I have been referring to Nathanael Schärli’s dissertation and concept paper introducing traits. One key feature that seems to be missing from most mixin and/or multiple inheritance implementations is the ability to rename methods when you import them to avoid collision/ambiguity. Can Scala do that? Steve McDowell One key different between mixins and traits

Is there a way to extend trait in PHP?

大憨熊 提交于 2019-12-03 06:28:33
问题 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. 回答1: Yes, there is. You just have to define new trait like this: trait MySoftDeletes { use SoftDeletes { SoftDeletes::saveWithHistory as

Scala immutable objects and traits with val fields

孤街醉人 提交于 2019-12-03 06:07:36
I would like to construct my domain model using immutable objects only. But I also want to use traits with val fields and move some functionality to traits. Please look at the following example: trait Versionable { val version = 0 def incrementVersion = copy(version=version+1) } Unfortunatelly such code doesn't work - copy method is unknown for trait Versionable. I think that it would be nice to have copy method generated for every trait and class. Such method should create shallow copy of object and return it using the same type as for original object with given field modified accoring to

Can I simulate traits/mixins in Swift?

梦想与她 提交于 2019-12-03 05:39: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? 回答1: 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

Get the signed/unsigned variant of an integer template parameter without explicit traits

杀马特。学长 韩版系。学妹 提交于 2019-12-03 04:55:15
I am looking to define a template class whose template parameter will always be an integer type. The class will contain two members, one of type T , and the other as the unsigned variant of type T -- i.e. if T == int , then T_Unsigned == unsigned int . My first instinct was to do this: template <typename T> class Range { typedef unsigned T T_Unsigned; // does not compile public: Range(T min, T_Unsigned range); private: T m_min; T_Unsigned m_range; }; But it doesn't work. I then thought about using partial template specialization, like so: template <typename T> struct UnsignedType {}; //

Using scala constructor to set variable defined in trait

早过忘川 提交于 2019-12-03 04:43:08
问题 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

Why does PHP have abstract classes if you can use an interface and traits?

大兔子大兔子 提交于 2019-12-03 04:22:38
问题 Earlier today I was doing research on PHP's abstract classes, interfaces, and traits. As far as I can tell, an abstract class says "anything using me will be using these methods and attributes", interfaces say "anything using me must have these methods and attributes", and traits say "anything using me will also have these methods and attributes". Now, my question is, if you get the equivalent of an abstract class when you use an interface and a trait, why are there abstract classes? If I'm

Mixing multiple traits in Scala

a 夏天 提交于 2019-12-03 04:04:31
问题 Quick note: Examples from the tutorial Scala for Java Refugees Part 5: Traits and Types . Suppose I have the traits Student, Worker, Underpaid, and Young. How could I declare a class ( not instance ), CollegeStudent, with all these traits? Note: I am aware of the simplests cases, such as CollegeStudent with one or two Traits: class CollegeStudent extends Student with Worker 回答1: It is easy, when declaring a class you just use the "with" keyword as often as you want class CollegeStudent

What are the pros of using traits over abstract classes?

浪子不回头ぞ 提交于 2019-12-03 02:59:44
问题 Can someone please explain traits in Scala? What are the advantages of traits over extending an abstract class? 回答1: 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(" ", ", ", "

stacking multiple traits in akka Actors

老子叫甜甜 提交于 2019-12-03 02:57:26
I'm creating multiple traits which extend Actor. Then I want to create an actor class which uses some of these traits. However, I'm not sure how to combine the receive methods from all traits in the receive method of the Actor class. Traits: trait ServerLocatorTrait extends Actor { def receive() = { case "s" => println("I'm server ") } } trait ServiceRegistrationTrait extends Actor { def receive() = { case "r" => println("I'm registration ") } } The Actor: class FinalActor extends Actor with ServiceRegistrationTrait with ServerLocatorTrait { override def receive = { super.receive orElse ??? <-