O'Reilly logo

Scala Puzzlers by Nermin Šerifovic, Andrew Phillips

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Puzzler 19

What's in a Name?

Relying on the precise order of items in a parameter list is fragile, especially if parameters are of the same type:

  def inEcosystem(predator: String, prey: String) { 
    println(s"${predator} eat ${prey}")
  }
    scala> inEcosystem("cats""mice")
  cats eat mice
    def inEcosystem(prey: String, predator: String) { 
    println(s"${prey} are eaten by ${predator}")
  }
    // no idea that the definition has changed...
  scala> inEcosystem("cats""mice")
  cats are eaten by mice
    scala> inEcosystem(predator = "cats", prey = "mice")
  mice are eaten by cats

Scala also supports default arguments,[1] making it easy to write versatile functions and provide for common use cases without a combinatorial explosion of overloaded methods:

  object ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required