What does the question mark mean in GSP/Grails?

前端 未结 4 471
没有蜡笔的小新
没有蜡笔的小新 2021-01-31 14:58

I saw this in my generated GSP pages. What does the ? mean?


4条回答
  •  佛祖请我去吃肉
    2021-01-31 15:41

    the safe navigation operator (?.) returns null if the object on the left is null, otherwise it returns the value of the right member of that object. so phoneInstance?.name is just shorthandn for phoneInstance == null ? null : phoneInstance.name

    for example:

    a = x?.y
    

    is just shorthand for:

    a = (x == null ? null : x.y)
    

    which is shorthand for:

    if(x == null){
        a = null
    } else {
        a = x.y
    }
    

提交回复
热议问题