score:3

Accepted answer

Overloading is not what the compiler is concerned about.

<console>:8: error: method parser: (identifier: String, default: String) String does not take type parameters.

This code:

parser[String](identifier, default, identity)

calls this method:

def parser(identifier: String, default: String): String

instead of this one as you would want:

def parser[T](identifier: String, default: T, modifier: String => T): T

This illustration compiles just fine:

val args = Array[String]()

def parser[T](identifier: String, default: T, modifier: String => T): T = {
  val l = args.filter(_.startsWith(identifier))
  if(l.isEmpty) default
  else modifier(l(0).drop(identifier.length).trim)
}

def parser(identifier: String, default: String): String = "dummy"

Related Query

More Query from same tag