what is this operator called and what is it used for <=>

后端 未结 4 969
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-18 23:00

I recently came across this magical operator when digging into Groovy: <=>

Groovy has really made me happy with elvis operators ?. and ?: which I use constantly

4条回答
  •  深忆病人
    2021-01-18 23:25

    Like many others mention, it's called the spaceship operator. Here's my test:

    def a
    def b
    
    println 1 <=> 0                 // 1
    println 0 <=> 1                 // -1
    println 1 <=> a                 // 1
    println b <=> 0                 // -1
    println a <=> b                 // 0
    println "abc" <=> "def"         // -1
    println "abc" <=> 1             // throw exception: java.lang.ClassCastException
    

提交回复
热议问题