tags:

views:

84

answers:

2

Hi, 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

+10  A: 

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) = {
Daniel
Worth mentioning that `()` *is* `Unit`
oxbow_lakes
+6  A: 

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.

pr1001