18: Traits - royal-lang/rl GitHub Wiki

Traits are similar to interfaces but are not types that are inherited.

A trait is used to share functionality between types that do not have inheritance etc.

They're also useful for types you do not control yourself but want to share functionality with.

A general naming convention for traits is to always have Trait as part of their name.

trait NAME
{
}

Example:

trait BazTrait
{
    fn baz();
}
ref struct Foo
{
    fn baz() { ... }
}

ref struct Bar
{
    fn baz() { ... }
}

ref struct Boo
{
    fn boo() { ... }
}
fn callBaz(BazTrait bazTrait)
{
    bazTrait.baz();
}
var foo @= Foo;
var bar @= Bar;
var boo @= Boo;

callBaz(foo); // Ok.
callBaz(bar); // Ok.
callBaz(boo); // Error: The type 'Boo' does not satisfy the trait 'BazTrait'