Skip to main content

Advanced concepts and explanations

Traits

A trait is really just a type that:

  • has a single value constructor wrapping one record.
  • the @[trait] attribute.

For example, Eq from the standard library is defined as:

pub trait Eq a where
def eq: a -> a -> Bool

The compiler really sees this definition as:

@[trait]
pub bare type Eq a = Eq {
eq: a -> a -> Bool,
}

The @[trait] attribute is necessary because it tells the compiler to descend into the record of this trait to search for other traits when resolving givens.

It is also necessary that the type is marked as bare so that other modules can access the functions defined for the trait.