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

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -