Questions tagged [traits]

In computer programming, a trait is a collection of methods, used as a "simple conceptual model for structuring object oriented programs"

In computer programming, a trait is a collection of methods, used as a "simple conceptual model for structuring object oriented programs".

Traits have the following properties

  • Provides a set of methods that implement behaviour.
  • Requires a set of methods that parameterize the provided behaviour.
  • Does not specify any state variables.
  • Never directly accesses state variables.

Can be composed

  • Trait composition is symmetric and conflicting methods are excluded from the composition.
  • Can be nested, but the nesting has no semantics for classes.
3441 questions
21
votes
2 answers

trait with functions that return an iterator

I'm trying to build a trait with functions that return an iterator. My simple example looks like this: pub trait TraitA { fn things(&self) -> Iterator; } fn foo(a: &A) { for x in a.things() { } } Which does not work…
ynimous
  • 4,642
  • 6
  • 27
  • 43
21
votes
8 answers

java traits or mixins pattern?

Is there a way to emulate mixins or traits in java? basically, I need a way to do multiple inheritance so I can add common business logic to several classes
joshjdevl
  • 7,092
  • 12
  • 45
  • 57
21
votes
3 answers

Scala single method interface implementation

Does Scala have any syntactic sugar to replace the following code: val thread = new Thread(new Runnable { def run() { println("hello world") } }) with something more like: val thread = new Thread(() => println("hello world")) in…
Mequrel
  • 727
  • 6
  • 12
21
votes
3 answers

Traits in javascript

How can I implement traits in javascript ?
Tinku
  • 1,592
  • 1
  • 15
  • 27
21
votes
2 answers

How do I best share behavior among Akka actors?

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? …
sdanzig
  • 4,510
  • 1
  • 23
  • 27
21
votes
2 answers

is it possible to add traits to a class in PHP in runtime?

Simple question, is it possible to dynamically add traits to a php class in runtime without using eval?
Thomas Lauria
  • 858
  • 1
  • 7
  • 15
21
votes
5 answers

PHP trait: is there a proper way to ensure that class using a trait extends a super class which contains certain method?

Example #2 from PHP manual http://php.net/manual/en/language.oop5.traits.php states
Maciej Sz
  • 11,151
  • 7
  • 40
  • 56
21
votes
1 answer

Scala's MapLike, ListLike, SeqLike, etc how does each compare to Map, List, Seq?

Could someone please help me understand Scala's various "Like" traits in the collection API. I've been reading over and trying to compare each without luck. I think I can see that Map for example, extends MapLike - adding 2 concrete methods. But…
LaloInDublin
  • 5,379
  • 4
  • 22
  • 26
20
votes
2 answers

Why isn't `std::mem::drop` exactly the same as the closure |_|() in higher-ranked trait bounds?

The implementation of std::mem::drop is documented to be the following: pub fn drop(_x: T) { } As such, I would expect the closure |_| () (colloquially known as the toilet closure) to be a potential 1:1 replacement to drop, in both directions.…
E_net4
  • 27,810
  • 13
  • 101
  • 139
20
votes
1 answer

Is there a way to alias multiple derives as a single one?

When using the newtype pattern I often have lengthy derives: extern crate derive_more; use derive_more::*; #[derive(Add, Sub, Mul, Div, ..., Copy, Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] struct Foo(i32); Is there a way to shorten this…
HiDefender
  • 2,088
  • 2
  • 14
  • 31
20
votes
1 answer

Mysterious lifetime issue while implementing trait for dyn object

Consider the following toy example: use std::cmp::Ordering; pub trait SimpleOrder { fn key(&self) -> u32; } impl PartialOrd for dyn SimpleOrder { fn partial_cmp(&self, other: &dyn SimpleOrder) -> Option { …
orlp
  • 112,504
  • 36
  • 218
  • 315
20
votes
1 answer

What is the difference between a Decorator, Attribute, Aspect, and Trait?

From the standpoint of pure computer science (or perhaps computational linguisticis), I would like to know the difference between the words: Decorator Attribute Aspect Trait Various Languages utilize these words and functionality in difference…
Darkenor
  • 4,349
  • 8
  • 40
  • 67
20
votes
2 answers

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

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.…
Blair Holloway
  • 15,969
  • 2
  • 29
  • 28
20
votes
3 answers

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

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…
user1846065
19
votes
1 answer

What is more Scala idiomatic: trait TraitA extends TraitB or trait TraitA { self: TraitB => }

Apart from the inheritance aspect, is there a difference between the following class templates: 1| trait TraitA extends TraitB 2| trait TraitA { self: TraitB => } I would like to split responsibilities between TraitA and TraitB but the former…
Tim Friske
  • 2,012
  • 1
  • 18
  • 28