Is it possible to extend a default method implementation of a trait in a struct?

This isn’t possible directly now. However, RFC 1210: impl specialization contains various aspects that will make this sort of behaviour work, for example, something like this should work: trait Foo { fn method(&self) { println!(“default implementation”); } } trait Bar: Foo { … } partial impl<T: Bar> Foo for T { default fn method(&self) { … Read more

Can traits be used on enum types?

Can traits be used on enum types? Yes. In fact, you already have multiple traits defined for your enum; the traits Debug, Copy and Clone: #[derive(Debug, Copy, Clone)] pub enum SceneType The problem is that you aren’t attempting to implement Playable for your enum, you are trying to implement it for one of the enum’s … Read more

Why can a Scala trait extend a class?

Yes they can, a trait that extends a class puts a restriction on what classes can extend that trait – namely, all classes that mix-in that trait must extend that class. scala> class Foo defined class Foo scala> trait FooTrait extends Foo defined trait FooTrait scala> val good = new Foo with FooTrait good: Foo … Read more

Mixins vs. Traits

Mixins may contain state, (traditional) traits don’t. Mixins use “implicit conflict resolution”, traits use “explicit conflict resolution” Mixins depends on linearization, traits are flattened. Lecture about traits ad 1. In mixins you can define instance variables. Traits do not allow this. The state must be provided by the composing class (=class using the traits) ad … Read more

How would you implement a “trait” design-pattern in C#?

You can get the syntax by using marker interfaces and extension methods. Prerequisite: the interfaces need to define the contract which is later used by the extension method. Basically the interface defines the contract for being able to “implement” a trait; ideally the class where you add the interface should already have all members of … Read more

Can I avoid eager ambiguity resolution for trait implementations with generics?

Can I avoid eager ambiguity resolution for trait implementations with generics? No. Is it possible to have [ambiguity resolution to be done at the call site, rather than at the definition site]? No. There’s a (long-delayed) RFC for specialization that will allow overlapping trait implementations, but only when one of them is more specific than … Read more

Cake pattern with overriding abstract type don’t work with Upper Type Bounds

It’s a shortcoming of Scala’s type system. When determining the members in a mixin, Scala uses two rules: First, concrete always overrides abstract. Second, If two members are both concrete, or both abstract, then the one that comes later in linearization order wins. Furthermore, the self type of a trait trait S { this: C … Read more