Documentation contents
Traits, generics and modules
Describe what a type can do with a trait, write functions that work for any type that fits, and decide what a module shares.
On this page
Myceta 2.0. These pages describe the language as planned for 2.0, the first public release, which is still in development. Details may change before release. The examples were type-checked and tested with the development compiler.
Traits
A trait is a set of methods a type can provide. A method written with a body is a default: types that implement the trait get it unless they supply their own.
trait Summarized {
fn summary(self: Self) -> text
fn headline(self: Self) -> text = "Station " ++ self.summary()
}
impl provides the trait for a type. Inside it, Self is that type.
type Station = { name: text; sensors: i64 } derive(Eq, Ord, Debug, Clone)
impl Summarized for Station {
fn summary(self: Self) -> text = self.name
}
Call a method with a dot. When you need to say which trait a method comes from, name the trait and the type.
node Harbor = Station { name = "Harbor"; sensors = 4 }
node Ridge = Station { name = "Ridge"; sensors = 9 }
node HarborSummary = Harbor.summary()
node HarborHeadline = Summarized::headline<Station>(Harbor)
Traits that build on others
A trait can require another. Any type that implements Ranked must also support Eq.
trait Ranked: Eq {
fn rank(self: Self) -> i64
fn outranks(self: Self, other: Self) -> bool = self.rank() > other.rank()
}
impl Ranked for Station {
fn rank(self: Self) -> i64 = self.sensors
}
Derived traits
For the common traits, derive writes the implementation for you. Eq gives equality, Ord gives ordering, Debug gives a printable form for inspection, and Clone gives copying.
node Ordering = Harbor.cmp(Ridge)
Generic functions and bounds
A bound restricts a type parameter to types that implement a trait, and lets the function use that trait's methods.
fn busier<T: Ranked>(a: T, b: T) -> T =
if a.outranks(b) then a else b
fn sameStation<T: Eq>(a: T, b: T) -> bool = a == b
node Busiest = busier(Harbor, Ridge)
node BusiestName = Busiest.name
node Same = sameStation(Harbor, Ridge)
A parameter with no bound accepts any type. The compiler normally works out the type argument from the call. You can also state it.
fn firstOf<T>(a: T, b: T) -> T = a
node First = firstOf<text>("left", "right")
Join several bounds with +, as in <T: Num + Ord>.
Modules
A source file declares one module. Module names are dotted and capitalized.
module Station.Traits
Export lines choose what other modules can use. A module with no export lines shares everything it declares.
export type Station
export fn busier
export node HarborSummary
Another module brings these in with import, or with from and a name of its own choosing:
from Myceta.Collections.Map import * as Map
A set of core modules is available everywhere without an import. The language reference lists them.
Run the example
The complete module is station-traits.myc, run by the test manifest. The next page covers units of measure.