Skip to main content

Traits

Trait allows you to define a set of methods that can be used in any type. This is similar to interfaces in C# or Java.

trait Num a =
sum :: a -> a -> a
prod :: a -> a -> a

methods can have a default implementation

trait Eq a =
(=) :: a -> a -> Bool
(!=) :: a -> a -> Bool

(=) a b = not (a != b)
(!=) a b = not (a = b)

Implementing traits

Using impl you can define the implementation of a trait for a type

impl Eq for Num =
(=) a b = ...

you can also use native when implementing the trait methos

impl Eq for Num =
native (=) a b

annotations also are allowed when implementing the trait methods

impl Eq for Num =
@name("equals" for=["java"])
(=) a b = False
Note

Impl are loaded when the module is imported.