1

scala 2.9.2 This compiles fine

object AppBuilder extends App {

  def app( blockw: Int => String ) : List[String] = List( blockw(6) )

  def app( block: => String ) : List[String] = app( _ => block )
}

But in REPL, the same methods/functions ( not sure about the distinction here) as above, when not enclosed in a class, I get the following errors

scala> def app( blockw: Int => String ) : List[String] = List( blockw(6) )
app: (blockw: Int => String)List[String]

scala> def app( block: => String ) : List[String] = app( _ => block )
<console>:8: error: missing parameter type
       def app( block: => String ) : List[String] = app( _ => block )
                                                     ^
smartnut007
  • 6,324
  • 6
  • 45
  • 52

1 Answers1

5

Overloaded methods aren't currently supported in the REPL—see this answer for an explanation of why. You can prove this with a much simpler example:

scala> def f(x: Int) = x
f: (x: Int)Int

scala> def f(x: String) = x
f: (x: String)String

Now try f(0), and you'll get a type mismatch.

You can use paste mode as a workaround:

scala> :paste
// Entering paste mode (ctrl-D to finish)

def app( blockw: Int => String ) : List[String] = List( blockw(6) )
def app( block: => String ) : List[String] = app( _ => block )

// Exiting paste mode, now interpreting.

app: (blockw: Int => String)List[String] <and> (block: => String)List[String]
app: (blockw: Int => String)List[String] <and> (block: => String)List[String]

But you'd probably be better off avoiding overloading.

Community
  • 1
  • 1
Travis Brown
  • 138,631
  • 12
  • 375
  • 680