Scala syntax which I don't understand -
i started scala course on coursera, can't 1 thing here:
trait generator[+t] { self => // able use self instead of def generate: t def map[s](f: t => s): generator[s] = new generator[s] { def generate = f(self.generate) } }
why using map[s]
not map
in function definition?
the [s]
after map
type parameter , makes so-called polymorphic method. in example above, if wrote same def map
, without [s]
, compiler wouldn't able tell s
when encountering in remaining method definition. [s]
makes identifier s
known compiler , puts in position report typos errors.
for example, assume new method in accidentally wrote f: t => floot
rather f: t => float
. want compiler complain floot
unknown identifier. wouldn't want silently assume floot
sort of type parameter.
Comments
Post a Comment