How is this type miss match?

Go To StackoverFlow.com

0

HI I am new at Scala Trying to run this code:

 class Number(x : Int){
        var number = x

        def inc(): Int = {
            number => number + 1
        }
  }

But I get the following error: solution.scala:12: error: missing parameter type number => number + 1 I dont know how to fix this.

2012-04-03 23:30
by Saeed Najafi
Note that you can skip an intermediate x: class Number(var number: Int) { … }elbowich 2012-04-04 02:31


0

or simply:

def inc = (x: Int) => x + 1

since Int return type is inferred, no need to specify it

As for dealing with mutability in the question, inc(1), inc(5), etc. are themselves transformed representations of the number passed to the class instance (i.e. they equate to "var number", but immutably so). No real need for mutability based on what we see here...

2012-04-04 08:45
by virtualeyes


2

Essentially, you can expicitly say what type you're expect:

def inc(): Int = {
            number: Int => number + 1
}

BUT this won't compile, cause what you've defined is function, so:

def inc(): (Int) => Int = {
  // some function that takes Int, calls it `number` and increment   
  number: Int => number + 1
}

would be closer,
BUT
it doesn't make sense and notice, that number you've defined has nothing in common with number variable inside class -- that's why Scala compiler cannot infer type for you.

I think you have wanted to write something like:

    def inc(): Int = {number += 1; number;}
    // will take effect on number field

or

    def inc(num: Int): Int = num + 1
2012-04-03 23:33
by om-nom-nom
I think it should be def inc() = { number = number + 1; number }, going with a mutable type (ick!) as seems to be the case - NoName 2012-04-03 23:43
@pst yeah, you right ; - om-nom-nom 2012-04-03 23:46
thanks guys really helped : - Saeed Najafi 2012-04-03 23:47
Ads