Is there an “opposite” to the null coalescing operator? (…in any language?)

后端 未结 12 1060
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 18:38

null coalescing translates roughly to return x, unless it is null, in which case return y

I often need return null if x is null, otherwise return x

12条回答
  •  不思量自难忘°
    2020-12-04 19:15

    If you've got a special kind of short-circuit boolean logic, you can do this (javascript example):

    return x && x.y;
    

    If x is null, then it won't evaluate x.y.

提交回复
热议问题