Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
32 lines (17 sloc) 1.02 KB

Traits

Similar to / also known as: interfaces, protocols

Traits

Intersection types

It is also possible to specify a combination of types, if the types are traits:

fn pythagoras(x: T, y: T) -> T where T = Add<T, Out=T> and Mul<T, Out=T>:
        x * x + y * y

Here T is a type that implements both Add + Mul.

Intersection types are not distinct concrete types (like records or unions), just bound on existing concrete types. You cannot create an instance of Add + Mul, but it is nonetheless very useful for type parameters.

Note that it does not make sense to create an intersection containing one or more known, concrete types.

  • Two concrete types does not make sense because an object cannot have two concrete types.
  • One concrete type with a trait does not make sense because a concrete type already implies a known set of traits per scope, so it is either always or never satisfied.
You can’t perform that action at this time.