At REPL, trigger method with one parameter without parentheses does not work

When I execute code at REPL, i.e at command prompt:

$ scala

Then, I test the ability of trigger a method without using parentheses
In case I created a class, and trigger the method via the object, than it works

`class AA { def in(t:String) = print(t) }`
`val obj = new AA()`
`obj in "Hi"`


But below code, which I create the method in runtime scope, it does not work:

def in(t:String) = print(t)
in "hi"

I think the method identifier always must be qualified for this style (hence the name “infix notation”). The additional problem is that AFAIK in the REPL there’s nothing to qualify in with, i.e. there’s no identifier for the synthetic top level object, so you’d have to explicitly wrap the method declaration in a dedicated object in order to use this style.

scala> object Foo { def in(t:String) = print(t) }
object Foo

scala> Foo in "a"

However, use of infix syntax is discouraged anyway, except for specific cases (operators, HOF).

1 Like