Wednesday, June 29, 2016
Scala Unit Explaination
http://stackoverflow.com/questions/14205367/found-unit-required-int-why-is-the-error-not-obvious
From the above link
From the above link
I'll first explain what type
Unit is, just in case. Even if you know already, other people having the same kind of problem might well not know it.
Type
Unit is similar to what is known in C or Java as void . In those languages, that means "this does not return anything". However, every method in Scala returns a value.
To bridge the gap between every method returning something and not having anything useful to return, there's
Unit . This type is an AnyVal , which means it isn't allocated on the heap, unless it gets boxed or is the type of a field on an object. Furthermore, it has only one value, whose literal is () . That is, you could write this:
The practical effect of this is that, when a method "returns"
Unit , the compiler doesn't have to actually return any value, since it already knows what that value is. Therefore, it can implement methods returning Unit by declaring them, on the bytecode level, as being void .
Anyway, if you don't want to return anything, you return
Unit .
Now let's look at the code given. Eclipse says it returns
Unit , and, in fact, Eclipse is correct. However, most people would actually make the error of having that method return AnyVal or Any , not Unit . See the following snippet for an example:
So, what happened? Well, when Scala finds an
if statement, it has to figure out what is the type returned by it (in Scala, if statements return values as well). Consider the following hypothetical line:
Clearly, the returned value will be either
x or y , so the type must be such that both x and y will fit. One such type is Any , since everything has type Any . If both x and y were of the same type -- say, Int -- then that would also be a valid return type. Since Scala picks the most specific type, it would pick Int over Any .
Now, what happens when you don't have an
else statement? Even in the absence of an else statement, the condition may be false -- otherwise, there would be no point in using if . What Scala does, in this case, is to add an else statement. That is, it rewrites that if statement like this:
Like I said before: if you do not have anything to return, return
Unit ! That is exactly what happens. Since both Int and Unit are AnyVal , and given that AnyVal is more specific than Any , that line returns AnyVal .
So far I have explained what others might have seen, but not what happens in that specific code in the question:
We have seen already that Scala will rewrite it like this:
We have also seen that Scala will pick the most specific type between the two possible results. Finally, Eclipse tells use that the return type is
Unit , so the only possible explanation is that the type of this:
is also
Unit . And that's precisely correct: statements that declare things in Scala have type Unit . And, so, by the way, do assignments.
The solution, as others have pointed out, is to remove the assignment and add an
else statement also returning an Int :
|
Tuesday, June 14, 2016
Friday, June 10, 2016
Subscribe to:
Posts (Atom)