Arnold deVos @a4dev
The talk explores when to use type members vs. type parameters through a series of examples.
Ritual airing of scala grievances.
trait Key
val a, b, c = new Keyval m =Map.empty.updated a, "hi".updated b, "there"m.geta == Some"hi"m.getc == None
trait Key[A]
val a = new Key[String]val b = new Key[Int]val m =HMap.empty.adda"ballons".addb99m.geta == Some"balloons"m.getb == Some99
Nada Amin, Samuel Grütter, Martin Odersky, Tiark Rompf, and Sandro Stucki
While hiking together in the French alps in 2013, Martin Odersky tried to explain to Phil Wadler why languages like Scala had foundations that were not directly related via the Curry-Howard isomorphism...
Exactly what Professor Wadler said is not recorded.
trait Key type A
val a = new Key type A = Stringval b = new Key type A = Intval m =HMap.empty.adda"balloons".addb99m.geta == Some"balloons"m.getb == Some99
With type parameters
def projectm0 HMap, ks Key[_]*: HMap = ???
With type members
def projectm0 HMap, ks Key*: HMap = ...
Can write the latter, not sure about the former.
Simone de Beauvoir and Jean-Paul Sartre
trait Rule type Value; ...def rulePfk Key pf PartialFunction[HMap, k.Value]: Ruledef applyRulesh HMap, rs List[Rule]: HMap
val rules = ListrulePfAttention case Titlea & Patientp => s" " ,rulePfAttention case Patientp => p ,rulePfBatchCount case Method"nogap" => 1 ,rulePfGST case FeeExGSTf => f * 0.1 ,rulePfGST case Amounta => a / 11.0 ,rulePfAmount case FeeExGSTf & GSTg => f+g ,rulePfFeeExGST case Amounta & GSTg => a-g ,rulePfOpeningBalance case Amounta => a ,rulePfBalance case OpeningBalancea & Paidp => a-p
type Context[S]trait Reducer[A, S]type Statedef init: Statedef applys State, a A: Context[State]def isReduceds State: Booleandef completes State: S
def educe[X, S] xs Iterator[X], f Reducer[X, S]: Context[S]
trait Transducer[A, B]def apply[S]fa Reducer[A, S]: Reducer[B, S]
declare types that should be exposed in parameters and types that should be hidden in members
declare a type (or type constructor) that is widely used in a member in an enclosing scope
What | Parameter | Member |
---|---|---|
nonvariant decl. | trait T[X] {} |
trait T { type X } |
nonvariant appl. | T[A] |
T { type X = A } |
covariant decl | trait T[+X] {} |
trait T { type X } |
covariant appl. | T[A] |
T { type X <: A } |
contravariant decl | trait T[-X] {} |
trait T { type X } |
contravariant appl. | T[A] |
T { type X >: A } |
existential | T[_ <: A] |
T { type X <: A } |
higher kind | trait T[X[Y]] |
trait T { type X <: { type Y }} |