I can define a bang operator method, but I cannot name it in Scala. What for?

First, I define a method !

:

scala> def !() = "hi"
$bang: ()java.lang.String

      

Now I can call it like this:

scala> $bang()
res3: java.lang.String = hi

      

But this doesn't work:

scala> !()
<console>:8: error: value unary_! is not a member of Unit
              !()

      

Even this doesn't work:

scala> `!`()
<console>:8: error: value unary_! is not a member of Unit
              `!`()
              ^

      

What am I doing wrong here? Why can I detect !()

when I cannot call it?

EDIT1

Adding an object reference gives an error:

scala> this.!()
<console>:8: error: value ! is not a member of object $iw
              this.!()
                   ^

      

+3


source to share


1 answer


!foo

      

interpreted as

foo.unary_!

      

If you want to call your method, you must provide an explicit receiver, eg.

this.!()

      



or

this !()

      

or

this ! ()

      

+1


source







All Articles