Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overloaded Methods Return Type for Type Ascription #43

Open
jeffusan opened this issue Apr 3, 2016 · 0 comments
Open

Overloaded Methods Return Type for Type Ascription #43

jeffusan opened this issue Apr 3, 2016 · 0 comments

Comments

@jeffusan
Copy link

jeffusan commented Apr 3, 2016

One of the Type Ascription hint-questions is:

Is it a recursive or overloaded methods return value? Yes, you have to.

It's easy to prove this for recursive methods:

scala> class SumMaker {
     |   def sum(ints: List[Int]) = ints match {
     |     case Nil => 0
     |     case x :: tail => x + sum(tail)
     |   }
     | }
<console>:40: error: recursive method sum needs result type
           case x :: tail => x + sum(tail)

scala> class SumMaker {
     |   def sum(ints: List[Int]): Int = ints match {
     |     case Nil => 0
     |     case x :: tail => x + sum(tail)
     |   }
     | }
defined class SumMaker

scala> val a = new SumMaker
a: SumMaker = SumMaker@545d7e85

scala> a.sum(List(1,2,3,4)
     | )
res3: Int = 10

But overloaded methods do not seem to require an ascribed return type:

scala> class Overloader {
     | def overloaded(x: Int) = { x }
     | def overloaded(x: String) = { x }
     | }
defined class Overloader

scala> val x = new Overloader()
x: Overloader = Overloader@10bdc63a

scala> x.overloaded("1")
res2: String = 1

scala> x.overloaded(1)
res3: Int = 1

Did I miss something? Or is ascribing the overloaded method's return value a recommendation rather than a requirement?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant