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

Can't implement a trait I don't own for all types that implement a trait I do own

pub trait AllValues { fn all_values() -> Vec where Self: std::marker::Sized; } use rand::Rand; use rand::Rng; impl Rand for T { fn rand(rng: &mut R) -> T { let values = T::all_values(); …
Ryan1729
  • 940
  • 7
  • 25
2
votes
2 answers

In Scala, is it possible for parent trait to call method implemented in child class?

I am using Scala, and wondering why this code works. trait Base { def foo(x: Int): Int } trait A extends Base { def fooA(x: Int): Int = { foo(x) } } class Impl extends Base with A { override def foo(x: Int): Int = x } val a = new…
invictus
  • 81
  • 5
2
votes
0 answers

Is it possible to inherit a trait where Self is only a type parameter of the trait?

I want to require that implementing trait A requires that f64 implements Add. One may call this reverse trait inheritance. I could use a where clause for this, but this has the downside that every time I want to use the trait I…
wiep
  • 172
  • 1
  • 7
2
votes
1 answer

php : Can trait precedence rules be used on properties?

Can trait precedence rules be used on properties? My initial research reveals nothing and tests have yielded no.... trait ReuseThis{ public $Dependency_Property; public function mutateProperty(){ //...long method } } trait…
user2782001
  • 3,380
  • 3
  • 22
  • 41
2
votes
2 answers

Error! Trait method restore has not been applied, because there are collisions with other trait methods on App\User- laravel softdelete

After using softdelete i got this error, how to fix softdelete with other trait.. class User extends Authenticatable { use HasApiTokens, Notifiable; use EntrustUserTrait { can as traitCan; hasRole as traitHasRole; } …
Binod Bhandary
  • 422
  • 1
  • 5
  • 22
2
votes
1 answer

When programming with 'idiomatic' Scala is the use of 'concrete classes' optional/redundant?

I've just moved into the functional programming domain and am working with Scala. After getting mentored with the Type Classes pattern by a senior engineer, I'm trying to wrap my head around the "need" for concrete classes when designing scala…
PhD
  • 11,202
  • 14
  • 64
  • 112
2
votes
0 answers

PHP Traits "triangle" dependency

we have a small pseudocode scenario in our project: trait headFunction { protected function doStuffWithParam($param){ return $param; } } trait extA { use headFunction; protected function doExtAStuff($param){ ... …
Filip
  • 212
  • 3
  • 12
2
votes
2 answers

Laravel 5.3-Limit login attempts default auth-Trait method hasTooManyLoginAttempts has not been applied

I am trying to implement limiting login attempts of default auth using ThrottleLogins trait Here is my implementation in Auth\LoginCotroller class LoginController extends Controller { use AuthenticatesUsers,ThrottlesLogins; /** * Where to…
Jabaa
  • 1,743
  • 7
  • 31
  • 60
2
votes
2 answers

impl convert::From for (mutable) reference

I'm trying to implement From for a type I want to get as a mutable reference, so I impl it for a &mut TheType, but then how do I properly call from? Attempts I performed fail because it tries to do reflexion (TheType from TheType) or can't (or don't…
GGalizzi
  • 813
  • 1
  • 10
  • 18
2
votes
2 answers

Laravel differents between autoload class & trait

I'm new in Laravel, I got a quest about helper (psr4 autoload class) & trait I create a autoload class, connect to api and do various things. My question is what's different between this and trait? I can use trait to do all the same things
Benjamin W
  • 2,658
  • 7
  • 26
  • 48
2
votes
1 answer

How to ensure structs implement functions consistently, without callers having to explicitly 'use' the trait?

Given multiple similar structs, it may be useful to implement functions matching a signature. The simple example below works nicely, but doesn't ensure all functions follow the same function signature. impl FooStruct { pub fn calc_value(seed:…
ideasman42
  • 42,413
  • 44
  • 197
  • 320
2
votes
1 answer

Json formatter for traits in Play 2.4

Am having a trait trait Role[A, B] { val _id: Option[A] = None val value: Option[List[B]] = None val id: Option[String] = None } And the case class extending the trait case class User (value1: Option[Role] = None, value2:…
sowmiyaksr
  • 169
  • 5
  • 18
2
votes
1 answer

Mixin and Trait based Compostion in JS

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.…
hilderic sb
  • 593
  • 1
  • 4
  • 13
2
votes
1 answer

Trait implementation with specific struct as parameter

I have a trait for readable and writable streams (like TcpStream): pub trait MyTraitPool { fn acquire(&self, &String) -> T; fn free(&self, T); } I want to implement that trait with TcpStream as T, so I would like to…
Maru
  • 894
  • 1
  • 12
  • 29
2
votes
1 answer

Sanity check on abstract val in a trait

I tried various things to do something quite simple that I would do using a decorator pattern in Java, but wanted to do with stackable modifications in Scala and failed. Here's my use-case: I'm implementing some simple auto-completers. They all…
Dici
  • 25,226
  • 7
  • 41
  • 82