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

Can Xcode8 customize a toolbar depending on size classes?

I want, using the storyboard, to add a UIBarButtonItem to a UIToolbar, but only for width:Regular and height:Regular devices. In the bottom of the storyboard, I activate „Vary for Traits“, and select width and height, so that only iPad devices…
Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116
2
votes
0 answers

Traits or Service Container

I have an upload method that may have different kinds of implementations (Switched to another different image service). I use that method on different controllers that needs it. I'm not sure if it's Service Container I need or a Trait and be…
jen
  • 218
  • 4
  • 13
2
votes
0 answers

xCode 8 all iPhones same size class so what to do?

Simpe layout with suggested constraints. one is iPhone 7+ other iPhone 4s. Both devices are deemed as width Compact height Regular so applying different traits with xcode 8s vary for Traits option would be pointless. How to acheive the quite obvious…
RyanTCB
  • 7,400
  • 5
  • 42
  • 62
2
votes
1 answer

How can I generalise a function that accepts an `&[T]` so that I can still invoke it with a bytestring literal?

I started off with a function a bit like this (playground): fn find (src: &[T], index: usize) -> T { // more complex algorithm, involving src goes here src[index] } pub fn main() { let x = b"abc"; assert_eq!(b'b', find(x,…
Peter Hall
  • 53,120
  • 14
  • 139
  • 204
2
votes
1 answer

How to force a trait to implement another traits

I have three traits, I would like to force one of them call it C to mix in the traits A and B. I can't do it even though I know how to force B to mix in A. It could be done like this: trait A { def getThree() = 3 } trait B { this: A => def…
GA1
  • 1,568
  • 2
  • 19
  • 30
2
votes
1 answer

PHP Trait Override Protected Trait Method

I am having problems with a composer package I am dealing with. It implements a trait Billable. trait Billable { /** * Update the payment method token for all of the user's subscriptions. * * @param string $token * @return…
Ben Ganley
  • 121
  • 1
  • 5
2
votes
1 answer

How to declare abstract method with array reference in PHP trait?

When attempting to use a class that exhibits a trait with an abstract method declared AND the abstract method is declared with a parameter that is of type reference to array I am getting the following error: PHP Fatal error: MyTrait and MyClass…
windsor
  • 379
  • 4
  • 9
2
votes
2 answers

How to implement PartialEq on Vector for my own structs?

I have the following definition: pub struct List { memory: Vec, } I would get the equivalent of #[derive(PartialEq)] for this type like describe in How can I implement PartialEq? I use a match expression, like: impl
LeMoussel
  • 5,290
  • 12
  • 69
  • 122
2
votes
1 answer

Specifying a templatized class with traits

I have a struct which indicates a trait: template struct FooTraits { static const NEbool s_implementsFoo = false; }; And I can specialize it with a class, thus: class Apple {}; template<> struct FooTraits< Apple > { static const…
davetapley
  • 17,000
  • 12
  • 60
  • 86
2
votes
1 answer

What does it mean if trait starts with self: Actor

I don't quite a good Scala programmer and need some help with understanding syntax. Here is the trait I am struggling with: trait ActorTracing extends AroundReceiveOverrideHack { self: Actor => protected def serviceName: String = …
user3663882
  • 6,957
  • 10
  • 51
  • 92
2
votes
1 answer

Codeigniter 3: How to use composer packages? (Twilio SDK)

What I did so far: I am pretty familiar with CI, but new to composer and the twilio SDK. Reading some tutorials and docs I managed to install composer and the twilio package. However the /vendor folder is parallel to my CI…
Steven M
  • 574
  • 4
  • 20
2
votes
1 answer

Can Groovy Trait Return Implementing Type Suitable for Method Chaining

Is it possible to incorporate trait methods along with methods on the implementing class using method chaining? This requires that the trait return the specific type of the implementing class, and the "this" variable is not that type by default.…
solvingJ
  • 1,321
  • 1
  • 19
  • 30
2
votes
1 answer

Casting &[u8] to std::io::Read causes Sized issue

Trying to provide &[u8] as argument to a function requiring Read doesn't seem to work as I expected, as illustrated by the below example. use std::io::Read; fn main() { let bytes: &[u8] = &[1, 2, 3, 4]; print_reader(&bytes); } fn…
Emanuel
  • 831
  • 6
  • 14
2
votes
0 answers

Grails 2.5.5 constraints do not importFrom traits

Given the following trait and domain class definitions: @grails.validation.Validateable trait SynchronizedData { String synchronizationId static constraints = { synchronizationId(blank: false, maxSize: 30, unique: true) …
Steve Hole
  • 348
  • 2
  • 12
2
votes
4 answers

Why Traits cannot be instantiated directly?

While testing for traits in PHP I was a bit confused why traits were introduced. I did some minor experiment. First I called trait methods directly in a class
Amit Ray
  • 3,445
  • 2
  • 19
  • 35
1 2 3
99
100