7

Possible Duplicate:
What is the rule for parenthesis in Scala method invocation?

I'm new to Scala, and I have some confusion with the () on postfix operator

I was told that toLong and toString are postfix operators for any integer, so I tried the following operations:

scala> 7 toString
res18: java.lang.String = 7

scala> 7.toString()
res19: java.lang.String = 7

scala> 7.toString
res20: java.lang.String = 7

scala> 7.toLong
res21: Long = 7 

scala> 7.toLong()
<console>:8: error: Long does not take parameters
              7.toLong()
                      ^

So, when shall one use "()" after an operator? Is there any pattern in that?

Big Thanks!

Community
  • 1
  • 1
Void Main
  • 2,241
  • 3
  • 27
  • 36

2 Answers2

14

First, it's probably better to think of toLong and toString as being methods on the Int class than postfix operators. Integer literals are objects in Scala, and thus, have methods, two of which are toLong and toString. (Admittedly, the situation is slightly more complex with Int because of implicit conversions, etc, but this is a good way to think about it as a beginner.)

So what are the rules for dropping parentheses? Scala syntax allows you to drop the () if the method doesn't take any arguments. However, if a method is defined without the parentheses, then they are not allowed at all:

class A() {
  def stuff() = "stuff"
  def things = "things"
}

val a = new A()

a.stuff     // fine
a.stuff()   // fine
a.things    // fine
a.things()  // error!

Finally, when do you drop parentheses and when do you keep them? By convention, in Scala we drop the parentheses for methods that have no side effects and keep them when there are side effects. Obviously this is just a style thing, but it gives an extra clue to readers about how your code works.

dhg
  • 52,383
  • 8
  • 123
  • 144
  • so, I got the error because the Int class defines the method toLong without the parentheses? – Void Main Mar 19 '12 at 02:54
  • I got it! Thank you dhg! – Void Main Mar 19 '12 at 02:55
  • @dhg : Can you please share an example of such side-effect ? I guess every mutator method has a side effect, and no accessor will have side-effect. Am I correct in my understanding ? – dev Oct 01 '13 at 07:04
5

See What is the rule for parenthesis in Scala method invocation? for detailed information.

Additionally Int.toLong is defined without parenthesis and therefore cannot be invoked with parenthesis. It is most likely defined without parenthesis because it does not have side effect (it's a convention).

Since toString() comes from Java, for interoperability it was defined with parenthesis and therefore can be invoked with or without parenthesis.

Community
  • 1
  • 1
huynhjl
  • 41,520
  • 14
  • 105
  • 158
  • 2
    I just discovered that `(123.toString())(1)` is `'2'` but `(123.toString)(1)` is an error, since it tries to pass the `1` as an argument to the `toString()` method. Kinda unexpected. – Luigi Plinge Mar 19 '12 at 04:48
  • @Luigi looks like a bug to me since I can't imagine a scenario in which the parser should attach the `(1)` to the `toString` before calling `toString` on `123` – dhg Mar 19 '12 at 06:17