Does Scala have guards?

前端 未结 4 1392
旧时难觅i
旧时难觅i 2020-12-09 14:48

I started learning scala a few days ago and when learning it, I am comparing it with other functional programming languages like (Haskell, Erlang) which I had some familiari

4条回答
  •  盖世英雄少女心
    2020-12-09 15:03

    The simple answer is no. It is not exactly what you are looking for (an exact match for Haskell syntax). You would use Scala's "match" statement with a guard, and supply a wild card, like:

    num match {
        case 0 => "Zero"
        case n if n > -1 =>"Positive number"
        case _ => "Negative number"
    }
    

提交回复
热议问题