Why is return type needed on overloaded scala function? -


  def tojson[t](obj: t) = {       gson.tojson(obj)   }    def tojson[t](list: seq[t]) = {       tojson(seqasjavalist(list))   } 

this doesn't compile. , that's documented feature (see answer):

when method overloaded , 1 of methods calls another. calling method needs return type annotation.

the question is: why?

from above link + additional thought colleagues, here possible reasons:

  • scala uses return type determine overloaded methods. case, , why neded? (java doesn't use return types, example)
  • partial functions - if 1 of methods doesn't have arguments , other 1 does, tojson() may viewed partial function, it's not whether return type string or function

i know it's best practice specify return type anyone, why above snippet not compiling, , if return type inference isn't enough, why there in first place?

might not main reason, note reason explicit parameter given as:

when method recursive.

the problem is, depending on return type of "calling" function, call can either namesake, or (i.e. recursive).

let's say:

trait c  class extends c  def a(obj: a) = {2} 

now consider:

def a[t <: c](obj: t): int = {  a(obj) }  a(new a) //an int, 2 

versus:

def a[t <: c](obj: t): = {  a(obj) }  a(new a) //infinite recursion 

since inferring return type of recursive function finite-time undecidable in general, inferring return type of "calling" function finite-time undecidable in general.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -