Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can define a bang operator method, but I can't call it in Scala. Why?

First I define ! method:

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

Now I can call it like so:

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

But this doesnt' 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 am I allowed to define !() when I can't invoke it?

EDIT1

Adding an object reference gives error:

scala> this.!()
<console>:8: error: value ! is not a member of object $iw
              this.!()
                   ^
like image 656
tuxdna Avatar asked Jan 24 '26 07:01

tuxdna


1 Answers

!foo

is interpreted as

foo.unary_!

If you want to call your method, you must specify an explicit receiver, e.g.

this.!()

or

this !()

or

this ! ()
like image 92
Jörg W Mittag Avatar answered Jan 26 '26 00:01

Jörg W Mittag



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!