traits

Collisions with other trait methods

▼魔方 西西 提交于 2019-11-28 06:16:31
How can I deal with traits with methods of same name? trait FooTrait { public function fooMethod() { return 'foo method'; } public function getRow() { return 'foo row'; } } trait TooTrait { public function tooMethod() { return 'too method'; } public function getRow() { return 'too row'; } } class Boo { use FooTrait; use TooTrait; public function booMethod() { return $this->fooMethod(); } } error, Fatal error: Trait method getRow has not been applied, because there are collisions with other trait methods on Boo in... What should I do about it? And also, with two same method names, how can I get

Scala client composition with Traits vs implementing an abstract class

风流意气都作罢 提交于 2019-11-28 06:15:57
I have read that with Scala, it is generally advised to use Traits instead of Abstract classes to extend a base class. Is the following a good design pattern and layout? Is this how Traits were intended to replace Abstract? client class (with def function1) trait1 class (overrides function1) trait2 class (overrides function1) specificClient1 extends client with trait1 specificClient2 extends client with trait2 Travis Brown I don't know what your source is for the claim that you should prefer traits over abstract classes in Scala, but there are several reasons not to: Traits complicate Java

In Scala; should I use the App trait?

房东的猫 提交于 2019-11-28 05:18:37
I've just started learning Scala and many of the tutorials that I'm following are using a combination of different representations for a main method. Aside from the familiar main method; there's also the use of traits App or Application . It appears that Application has been deprecated and is not recommended, but I can't find any information that explains much beyond this about each of these ways to define an entry point. So, I'm wondering if someone could explain to me: How do the traits App and Application work? Why is the trait Application no longer recommended and what does the App trait

Can traits be used on enum types?

本秂侑毒 提交于 2019-11-28 03:50:02
问题 I read through the trait documentation and found a neat definition for using traits on structs. Is it possible to use traits on enum types? I have seen answers that say no, but they are 3 years old and don't quite do what I'm trying to do. I tried to do this: #[derive(Debug, Copy, Clone)] pub enum SceneType { Cutscene, Game, Menu, Pause, Credits, Exit, } //We want to guarantee every SceneType can be played statically trait Playable { fn play(); } impl Playable for SceneType::Cutscene { fn

Difference between Abstract Class and Trait [duplicate]

99封情书 提交于 2019-11-28 02:53:34
Possible Duplicate: Scala traits vs abstract classes What is the conceptual difference between abstract classes and traits? A class can only extend one superclass , and thus, only one abstract class. If you want to compose several classes the Scala way is to use mixin class composition : you combine an (optional) superclass, your own member definitions and one or more traits . A trait is restricted in comparison to classes in that it cannot have constructor parameters (compare the scala reference manual ). The restrictions of traits in comparison to classes are introduced to avoid typical

How can I implement Rust's Copy trait?

北慕城南 提交于 2019-11-28 02:34:20
问题 I am trying to initialise an array of structs in Rust: enum Direction { North, East, South, West, } struct RoadPoint { direction: Direction, index: i32, } // Initialise the array, but failed. let data = [RoadPoint { direction: Direction::East, index: 1 }; 4]; When I try to compile, the compiler complains that the Copy trait is not implemented: error[E0277]: the trait bound `main::RoadPoint: std::marker::Copy` is not satisfied --> src/main.rs:15:16 | 15 | let data = [RoadPoint { direction:

Compostions and mixins in JS

只谈情不闲聊 提交于 2019-11-28 01:40:38
I got some trouble with compostions and mixin . For examples, lets imagine that we have a AHero and Hero1 object. All heroes can move, so AHero.move() is a thing. And now, at a moment of the dev, I want to add the possibility to stun and beStunned. So I make a new object that look like this ( called stunTrait) : { stunned : false, stun(), beStun, } And I decide to give to possiblity to heroes to stun and beStunned. So I do a mixin : class AHero extends stunTrait(superclass). Now I want that a stunned hero can not move. So in Ahero : move(){ if(this.stunned) return; ... } Later I find out stun

Scala, Extend object with a generic trait

删除回忆录丶 提交于 2019-11-28 01:10:18
I'm using Scala and I want to extend a (singleton) object with a trait, which delivers a data structure and some methods, like this: trait Tray[T] { val tray = ListBuffer.empty[T] def add[T] (t: T) = tray += t def get[T]: List[T] = tray.toList } And then I'll would like to mix-in the trait into an object, like this: object Test with Tray[Int] But there are type mismatches in add and get : Test.add(1) // ... How can I'll get this to work? Or what is my mistake? Travis Brown The problem is that you're shadowing the trait's type parameter with the T on the add and get methods. See my answer here

How do I create an instance of a trait in a generic method in scala?

泪湿孤枕 提交于 2019-11-28 00:04:11
I'm trying to create an instance of a trait using this method val inst = new Object with MyTrait This works well, but I'd like to move this creation in to a generator function, ie. object Creator { def create[T] : T = new Object with T } I'm obviously going to need the manifest to somehow fix the type erasure problems, but before I get to this, I run in to 2 questions : Even with an implicit manifest, Scala still demands that T be a trait. How do I add a restriction to create[T] so that T is a trait? If I chose to use the Class.newInstance method to create the instance dynamically rather than

How does curly braces following trait instantiation work?

一曲冷凌霜 提交于 2019-11-27 23:38:45
问题 I find some confusing use of trait in some unittesting code, such as: trait MyTrait { val t1 = ... //some expression val t2 = ... //some expression } And then instantiate the trait using new and meanwhile some expressions wrapped by curly braces followed the instantiation. test("it is a test") { new MyTrait { // do something with t1 and t2 } } I am confused by this strange syntax. My question is: why use follow trait instantiation by curly braces? what is the purpose of trait instantiation in