Trying extension methods in the REPL

I decided to try extension methods in the REPL, but I can’t seem to get even the simplest example working. What am I doing wrong here with this trivial example?

Welcome to Scala 3.4.0 (17.0.7, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
                                                                                                              
scala> extension(i: Int)
     | def sqr = i * i
-- Error: ----------------------------------------------------------------------------------------------------
4 |def sqr = i * i
  |^^^
  |Extension without extension methods

You need to add indentation to the second line for the def to be part of the extension. (Otherwise the extension is effectively empty)

Welcome to Scala 3.3.1 (21.0.1, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
                                                                                
scala> extension(i: Int)
     |   def sqr = i * i
     | 
def sqr(i: Int): Int
                             

[/quote]

Duh! I had assumed without even realizing it that the continuation lines were already in the inner scope. Maybe the error message should provide a clue about indentation for all the dunces out there.

1 Like