traits
How do I implement a trait I don’t own for a type I don’t own?
While you can’t do that exactly, the usual workaround is to just wrap the type you want in your own type and implement the trait on that. use somecrate::FooType; use somecrate::BarTrait; struct MyType(FooType); impl BarTrait for MyType { fn bar(&self) { // use `self.0` here } }
How can I dispatch on traits relating two types, where the second type that co-satisfies the trait is uniquely determined by the first?
Certainly! Here’s the complete code incorporating the approaches discussed: using Traits @traitdef IsProduct{X} begin isnew(X) -> Bool coolness(X) -> Float64 end @traitdef IsProductWithMeasurement{X,M} begin @constraints begin istrait(IsProduct{X}) end measurements(X) -> M end type Rope color::String age_in_years::Float64 strength::Float64 length::Float64 end type Paper color::String age_in_years::Int64 content::String width::Float64 height::Float64 end function isnew(x::Rope) x.age_in_years < 10.0 end function coolness(x::Rope) … Read more
Difference between trait inheritance and self type annotation
Self type annotations allow you to express cyclic dependencies. For instance: trait A extends B trait B { self: A => } This is not possible with simple inheritance.
How can I create an is_prime function that is generic over various integer types?
Generic number types can be quite a nuisance to work with, but once you get the hang of them they don’t tend to be too bad, though a little more verbose. The standard building blocks to such methods are the traits in the num crate from crates.io, most notably Num, Zero and One, as well … Read more
The trait cannot be made into an object
You can either add a type parameter to your struct, as in Zernike’s answer, or use a trait object. Using the type parameter is better for performance because each value of T will create a specialized copy of the struct, which allows for static dispatch. A trait object uses dynamic dispatch so it lets you … Read more
Using scala constructor to set variable defined in trait
trait Foo { var foo: String = _ } class Bar(foo0: String) extends Foo { foo = foo0 } The trait declares an uninitialized var; the class then sets it equal to the input parameter. Alternatively, trait Foo { def foo: String def foo_=(s: String): Unit } class Bar(var foo: String) extends Foo {} declares … Read more
Why would I implement methods on a trait instead of as part of the trait?
When you define a trait named Foo that can be made into an object, Rust also defines a trait object type named dyn Foo. In older versions of Rust, this type was only called Foo (see What does “dyn” mean in a type?). For backwards compatibility with these older versions, Foo still works to name … Read more
How to mix-in a trait to instance?
I have a idea for this usage: //if I had a class like this final class Test { def f = println(“foo”) } trait MyTrait { def doSomething = { println(“boo”) } } object MyTrait { implicit def innerObj(o:MixTest) = o.obj def ::(o:Test) = new MixTest(o) final class MixTest private[MyTrait](val obj:Test) extends MyTrait } you … Read more
Linearization order in Scala
An intuitive way to reason about linearisation is to refer to the construction order and to visualise the linear hierarchy. You could think this way. The base class is constructed first; but before being able of constructing the base class, its superclasses/traits must be constructed first (this means construction starts at the top of the … Read more