how to convert a value when using it? Example:
scala> def sum(x:Double, y:Int) {
| x + y
| }
sum: (x: Double,y: Int)Unit
scala> println(sum(2,3))
()
How to modify the line with println to print the correct number?
Thanks
Note that sum
returns Unit
:
sum: (x: Double,y: Int)Unit
This happens because you missed an equal sign between method declaration and body:
def sum(x:Double, y:Int) {
You should have declared it like this:
def sum(x:Double, y:Int) = {
Your problem is not with casting but with your function definition. Because you ommitted the =
before the function parameters and the function body, it returns Unit
(i.e. nothing), as the REPL told you: sum: (x: Double,y: Int)
Unit. Simply add the equals:
def sum(x: Double, y: Int) = {
x + y
}
Now your sum
method will return a Double
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With