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
2
votes
1 answer

Immutable data on a trait

I'm trying to define a trait with a method returning some data, but this data must not be changed by the impl, i.e. it can only be set once and remain in that value for the lifetime of the impl. Is there any way to ensure this? Here's how I would…
AlphaModder
  • 3,266
  • 2
  • 28
  • 44
2
votes
1 answer

How did anonymous function implements the trait?

let's see the code in scala REPL: first, i defined a trait: trait Service{ def invoke(name:String):String } then i defined an anonymous function: def serviceImpl:Service = (name)=> s"Your name is $name" It works fine. the serviceImpl method…
john
  • 67
  • 3
2
votes
1 answer

Always eager load relation when using a trait in eloquent model

I'm providing a HasTranslation-Trait for any of my eloquent models. All models using this trait will receive a one-to-many-relation like this (where you can see my basic Model to ModelLanguages relations): public function languages() { return…
patriziotomato
  • 591
  • 1
  • 11
  • 25
2
votes
0 answers

In Rust, how can I abstract over a trait that has an associated type?

I'm writing a downloader for different hosts/providers and need to abstract over the different meta information they provide for files. For example, one host might return an MD5 sum, another an SHA1 sum or even no checksum at all. I wrote a Hoster…
Danyel
  • 2,180
  • 18
  • 32
2
votes
1 answer

How do you abstract generics in nested Rust types?

struct Disk { handle: T, } struct Partition { disk: Disk, } struct File { partition: Partition, } At the point of struct Partition, it is no longer…
eddy
  • 488
  • 5
  • 18
2
votes
6 answers

code reducing by using traits and the with-keyword

I have some classes with the same super-type. Therefore all of this classes have to override the same methods. Now I can call a method and commit it an object of the common super-type. But it is not always useful to react to each committed type…
kiritsuku
  • 52,967
  • 18
  • 114
  • 136
2
votes
1 answer

Scala: Let trait depend on other trait

I want to write a family of traits whose methods should log something and a Logger trait that should be implemented in concrete Loggers and it should only be possible to mix in the above traits when a Logger is mixed in as well. I only know that a…
Sebastian Bechtel
  • 363
  • 1
  • 3
  • 12
2
votes
1 answer

Undefined constant Traits in PhpStorm 2016.2.1

I am developing with Laravel 5.3 and everything works fine but PhpStorm keeps squawking at the namespace every time I use "Traits", like this: Please note that the code works fine, no errors whatsoever so I'm guessing this is a PhpStorm's issue.
DanVeira
  • 361
  • 1
  • 6
  • 15
2
votes
1 answer

Why can't I call a method with parameter `this` in a class extending a generic trait?

I'm writing a trait to describe node-like objects in a Hierarchical structure (like a graph). My working code is below: trait Hierarchical[T <: Hierarchical[_]] { // The parents will be a list of some arbitrary Hierarchical val parents:…
erip
  • 16,374
  • 11
  • 66
  • 121
2
votes
2 answers

Usage of noexcept in derived classes

I encounter an issue while using the noexcept specifier on derived classes, more precisely when the parent is an abstract class (has protected constructors). Hereafter is an example of the way I declare my classes. With a public constructor in the…
S.Clem
  • 491
  • 5
  • 10
2
votes
1 answer

Possible to have a struct/tuple of traits in Rust?

I understand this is illegal in Rust: trait A { } struct S { a: A, b: A } The reason is that all members except the last one must be sized, and a trait is not sized. Does this mean it is impossible to have a struct/tuple of traits? How would I…
rityzmon
  • 1,945
  • 16
  • 26
2
votes
0 answers

When should I write an implementation of the Drop trait for a type?

I am going through the too many lists book which explains the basics of Rust by writing some linked lists. In this chapter, the author explains why we might want to implement the Drop trait for our custom type stating that the automatic handling of…
2
votes
1 answer

Preventing value from dropping inside generic match

When attempting to create objects that inherit a trait from within a match statement, I find it's necessary to define different external variables for every type of object being matched. It's necessary to declare the variables outside of the match…
Ameo
  • 2,307
  • 5
  • 21
  • 33
2
votes
2 answers

What does "case class extends trait" mean in Scala?

I am understanding my existing project, few things I am not able to understand: trait PeriodA { def start: Long def stop: Long def description: String def manageTo: String } case class PeriodEntity( start: Long, stop: Long, …
Sun
  • 3,444
  • 7
  • 53
  • 83
2
votes
1 answer

How do I call a trait method on a reference and not the referenced value?

I have a reference (foo) that implements the trait IntoIterator. I want to call the trait method into_iter directly on foo (i.e. the reference itself) and not *foo (the referenced value), as seems to be happening, since the type of *foo…
Noldorin
  • 144,213
  • 56
  • 264
  • 302
1 2 3
99
100