Scala: type mismatch; found : Unit required: Boolean

后端 未结 1 1369
死守一世寂寞
死守一世寂寞 2021-02-19 02:09

Hi I\'m just trying out my first bits of scala and have hit this error which I don\'t understand. I\'ve been trying to work it out and have exhausted my ideas. Help?

<         


        
1条回答
  •  有刺的猬
    2021-02-19 02:44

    You have to have an else clause, otherwise the type checker doesn't know what the return type is when it's not the case that count<0.

    def calculate(count: Int): Boolean =    
      if (count<0) false
      else true
    

    Or, better yet, you don't need the if-statement at all:

    def calculate(count: Int) = count >= 0
    

    0 讨论(0)
提交回复
热议问题