Shortcut to make case/switch return a value

后端 未结 4 1102
一生所求
一生所求 2020-12-01 22:28

I\'m pretty sure I saw someone do a shortcut technique like the code below (which doesn\'t work)

return case guess
  when guess > @answer then :high
  whe         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 22:38

    Picking up on @mu's first comment on the question (an approach that looks fine to me), you could of course also write that as:

    return case (guess <=> @answer)
           when -1 then :low
           when  0 then :correct
           when  1 then :high
           end
    

    or

           ...
           else
             :high
           end
    

提交回复
热议问题