module Station.Traits export type Station export fn busier export node HarborSummary trait Summarized { fn summary(self: Self) -> text fn headline(self: Self) -> text = "Station " ++ self.summary() } trait Ranked: Eq { fn rank(self: Self) -> i64 fn outranks(self: Self, other: Self) -> bool = self.rank() > other.rank() } type Station = { name: text; sensors: i64 } derive(Eq, Ord, Debug, Clone) impl Summarized for Station { fn summary(self: Self) -> text = self.name } impl Ranked for Station { fn rank(self: Self) -> i64 = self.sensors } fn busier(a: T, b: T) -> T = if a.outranks(b) then a else b fn sameStation(a: T, b: T) -> bool = a == b fn firstOf(a: T, b: T) -> T = a node Harbor = Station { name = "Harbor"; sensors = 4 } node Ridge = Station { name = "Ridge"; sensors = 9 } node HarborSummary = Harbor.summary() node HarborHeadline = Summarized::headline(Harbor) node Busiest = busier(Harbor, Ridge) node BusiestName = Busiest.name node Same = sameStation(Harbor, Ridge) node Ordering = Harbor.cmp(Ridge) node First = firstOf("left", "right") test TraitsBehave { expect HarborSummary : OK with value = "Harbor" expect HarborHeadline : OK with value = "Station Harbor" expect BusiestName : OK with value = "Ridge" expect Same : OK with value = false expect First : OK with value = "left" }