1

Here is the example. I would like to know why I am not able to use Rounded Bracket (()) to call the toFloat method

This works : toFloat

scala> val no = "8.5".toFloat
no: Float = 8.5

This DO NOT work : toFloat()

scala> val no = "8.5".toFloat()
<console>:27: error: Float does not take parameters
         val no = "8.5".toFloat()
                               ^
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
Raj
  • 2,368
  • 6
  • 34
  • 52

1 Answers1

4

The method StringLike.toFloat is defined as:

def toFloat: Float = java.lang.Float.parseFloat(toString)

The documentation says the following in regards to parenthesis:

Parentheses

Unlike Ruby, Scala attaches significance to whether or not a method is declared with parentheses (only applicable to methods of arity-0).

For example:

def foo1() = ...

def foo2 = ...

These are different methods at compile-time. While foo1 can be called with or without the parentheses, foo2 may not be called with parentheses.

This goes to show that if a method is declared without parenthesis they may not be added during invocation.

Community
  • 1
  • 1
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321