lundi 20 avril 2015

any advantage to using declaring a method arg w/ paramaterized type vs declaring the arg as 'Any' in scala?

I recently ran across this example, which prints [7]

class Decorator(left: String, right: String) {
  def layout[A](x: A) = left + x.toString() + right
}


def apply(f: Int => String, v: Int) = f(v)
val decorator = new Decorator("[", "]")
println(apply(decorator.layout, 7))

It would also be possible to declare Decorator like this:

class Decorator(left: String, right: String) {
  def layout(x: Any) = left + x.toString() + right
}

Im curious to know: What does the first approach buy you ? thanks in advance !

-chris

Aucun commentaire:

Enregistrer un commentaire