Either should work. In scala you can omit .
with space for method invocation, which is probably broken version of haskell way. I think space is not recommended in scala as per official doc - STYLE GUIDE - METHOD INVOCATION
Example:
scala> val data = "guitar"
data: String = guitar
scala> data.toUpperCase
res8: String = GUITAR
you can use space instead of .
,
scala> data toUpperCase
<console>:13: warning: postfix operator toUpperCase should be enabled
by making the implicit value scala.language.postfixOps visible.
This can be achieved by adding the import clause 'import scala.language.postfixOps'
or by setting the compiler option -language:postfixOps.
See the Scaladoc for value scala.language.postfixOps for a discussion
why the feature should be explicitly enabled.
data toUpperCase
^
res9: String = GUITAR
Since .toUpperCase
is used after the data-structure without .
(it is called postfix Operator) and compiler is simply warning about that. It can be fixed as the warning says,
scala> import scala.language.postfixOps
import scala.language.postfixOps
scala> data toUpperCase
res10: String = GUITAR
Same thing applies to your example. From readability perspective dot makes more readable in your example.
scala> (1 to 10 by 3).toList
res9: List[Int] = List(1, 4, 7, 10)
ALSO, post-fix operators could lead into bugs. For example, 1 + "11" toInt
apply the toInt
at the end of the computation but maybe what I wanted was 1 + "11".toInt
ALSO, ALSO regarding your bug you need to yell at compiler to stop chaining on toList
with ;
or a new line after post-fix operator. Or you can use val
, def
after post-fix operator that way compiler knows it is different context now.
scala> :paste
import scala.language.postfixOps
val range = 1 to 10 by 3 toList
println(s"$range")
// Exiting paste mode, now interpreting.
List(1, 4, 7, 10)
import scala.language.postfixOps
range: List[Int] = List(1, 4, 7, 10)
Also read: Let’s drop postfix operators