Type inference on method return type

后端 未结 4 539
情话喂你
情话喂你 2020-12-03 06:45

Why does Scala fail to infer the return type of the method when there\'s an explicit return statement used in the method?

For instance, why does the fol

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-03 07:25

    Given this (2.8.Beta1):

    object Main {
      def who = return 5
      def main(args: Array[String]) = println(who)
    }
    :5: error: method who has return statement; needs result type
             def who = return 5
    

    ...it seems not inadvertent.

提交回复
热议问题