You cannot use a negative number in named parameters in Scala

I am using Scala 2.11.2.

If I have this fraction class:

case class Fraction(numerator: Int, denominator: Int) {} 

Then it gives an error:

 val f = new Fraction(numerator=-1, denominator=2) 

But this is not so:

 val f = new Fraction(-1, denominator=2) 

Error message:

 Multiple markers at this line - not found: value numerator - not found: value numerator 

I tried to use negative numbers in other fragments with the same result, but the documentation does not mention that this is not possible.

Am I doing something wrong?

thanks

+6
source share
1 answer

You need space between = and - , or you can wrap -1 in parentheses, otherwise the compiler gets confused. This is because =- is a valid method name, so the compiler cannot determine whether you assign a value to a named parameter or call a method call.

therefore this gives an error:

 val f = Fraction(numerator=-1, denominator=2) 

but it normal:

 val f = Fraction(numerator = -1, denominator = 2) 

and so on:

 val f = Fraction(numerator=(-1), denominator=2) 
+12
source

Source: https://habr.com/ru/post/977853/


All Articles