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
24
votes
2 answers

How do I make a struct callable?

#![feature(unboxed_closures)] #![feature(fn_traits)] struct foo; impl std::ops::Add for foo { type Output = foo; fn add(self, x: foo) -> foo { println!("Add for foo"); x } } impl Fn for foo { extern "rust-call" fn…
24
votes
1 answer

Why does io::copy require the reader and writer to both be mutable references?

Why does std::io::copy require that both the reader and writer arguments need to be passed as mutable references? I can understand why the writer needs to be mutated in order to accommodate data being written to it, changing its internal…
Ralph Caraveo
  • 10,025
  • 7
  • 40
  • 52
24
votes
2 answers

Initializing an anonymous class with a trait

Can someone help me understand the following behavior? Simply put: what is the difference between the following two cases where... I define a simple class c + trait t scala> class c {val x=true; val y=this.x} defined class c scala> trait t…
Bosh
  • 8,138
  • 11
  • 51
  • 77
23
votes
1 answer

Rust Error: The size for values of type `(dyn std::error::Error + 'static)` cannot be known at compilation time

first of all I want to mention that there many similar question on StackOverflow and around the web but I just can't figure out how to solve this error for my case. So I have a struct, which represents my own error type: #[derive(Debug)] pub struct…
Samuel Dressel
  • 1,181
  • 2
  • 13
  • 27
23
votes
1 answer

Can traits have properties & methods with private & protected visibility? Can traits have constructor, destructor & class-constants?

I've never seen a single trait where properties and methods are private or protected. Every time I worked with traits I observed that all the properties and methods declared into any trait are always public only. Can traits have properties and…
user9059272
23
votes
2 answers

When is it appropriate to mark a trait as unsafe, as opposed to marking all the functions in the trait as unsafe?

Saying the same thing in code, when would I pick either of the following examples? unsafe trait MyCoolTrait { fn method(&self) -> u8; } trait MyCoolTrait { unsafe fn method(&self) -> u8; } The opt-in builtin traits (OIBIT) RFC states: An…
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
23
votes
3 answers

PHP: When to use Traits and when to use static methods?

My PHP applications are generally using classes for namespacing. The methods within these classes are defined as static. Now that PHP has introduced Traits, I'm trying to wrap my head around when to use them. I saw some examples of using traits,…
KrekkieD
  • 967
  • 9
  • 23
23
votes
2 answers

php trait using another trait

I have a trait which is using another trait, and now I'm getting errors about functions that don't exist in classes. I have simplified the code: settings.php:
user936965
22
votes
3 answers

Why does Rust not allow the copy and drop traits on one type?

From the book: Rust won’t let us annotate a type with the Copy trait if the type, or any of its parts, has implemented the Drop trait. If the type needs something special to happen when the value goes out of scope and we add the Copy annotation to…
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
22
votes
1 answer

"Subclassing" traits in Rust

I have a situation where several of my structs should implement multiple traits, but all of them implement at least one trait in common. When I get hold of a mixed bag of these structs, I want to treat them all as being of the common trait: pass…
Dan Wiebe
  • 449
  • 1
  • 4
  • 11
22
votes
2 answers

When should I not implement a trait for references to implementors of that trait?

If I have a trait, and a function that accepts a generic type constrained to that type, everything works fine. If I try to pass in a reference to that type, I get a compilation error. trait Trait { fn hello(&self) -> u32; } struct…
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
22
votes
3 answers

How do you return an Iterator in Scala?

What must I do in order to be able to return an Iterator from a method/class ? How would one add that trait to a class?
Geo
  • 93,257
  • 117
  • 344
  • 520
22
votes
4 answers

How does curly braces following trait instantiation work?

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…
xiaohan2012
  • 9,870
  • 23
  • 67
  • 101
21
votes
1 answer

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

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?
user942821
21
votes
3 answers

Are there scala-like mixins for C++?

Scala Mixins
Łukasz Lew
  • 48,526
  • 41
  • 139
  • 208