33 lines
846 B
Plaintext
33 lines
846 B
Plaintext
/* Some basic interfaces, representing some useful functional concepts. */
|
|
|
|
/**
|
|
* Represents a "zero" for addition, for example, or a "one" for multiplication, for example
|
|
*/
|
|
pub int Identity<A> {
|
|
identity: A // a constant prop. By default, all props are immutable.
|
|
}
|
|
|
|
/**
|
|
* This shows how to do operator overloading via an interface
|
|
*/
|
|
pub int Addable<T : Addable<T>> {
|
|
fn op+(other: T): T // a function which must be implemented by inheritors
|
|
}
|
|
|
|
/**
|
|
* Represents a struct that can do concatenation of values.
|
|
*/
|
|
pub int Monoid<A> : Identity {
|
|
fn concat(first: A, second: A): A
|
|
}
|
|
|
|
decl pub int Number : Addable<Number> // primitive
|
|
|
|
// example usage
|
|
decl fn <T> assert_eq(expected: T, actual: T)
|
|
|
|
let add_monoid: Monoid<Number> = Monoid {
|
|
identity: 0
|
|
impl fn concat(a, b) = a + b
|
|
}
|
|
assert_eq(2, add_monoid.concat(1, 1)) |