KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
The Scala compiler currently cannot infer return types of recursive methods as in the following code def foo(i:Int) = if (i > 0) foo (i-1) else 0 Is there any ambuigity in the above statement? (i.e., is any type other than Int possible?) I can imagine that in more complex example, it will be difficult to infer the type. Is it possible to further characterize the cases of recursive methods where we can(not) infer the types? [EDIT:] The compiler is intelligent enough to figure out that String is incorrect. scala> def foo(i:Int):String = if (i > 0) foo (i-1) else 0 <console>:5: error: type mismatch; found : Int(0) required: String
Tags (comma-separated)
Save Edits
Cancel