链接空安全运算符
Chaining Null-Safe Operator
我的项目有如下代码:
params.stringValue?.trim().replaceAll('aa', 'a')
我们预计如果 params.stringValue
为 null,则 trim()
和 replaceAll()
都不会被调用。
然而我们在这一行得到了一个 NullPointerException
说 replaceAll()
不能在空对象上调用。
我们不得不将代码改成这样:
params.stringValue?.trim()?.replaceAll('aa', 'a')
为什么上面的第一个代码片段不起作用?这是 Groovy 中的一个错误,它会在遇到 null 一次后继续计算表达式吗?
这确实是 Groovy 的工作方式,并且已经被咬 others:
println book?.author?.firstName?.trim().concat(" is great.")
...
Looking at this line of code, I thought for sure I was safe from any sneaky NullPointerException. If book, author or firstName are null I'll simply end up printing null and not have to worry about the concat() method. After all, if the trim() method succeeds, there's no sense in guarding it's result for null. And that's where I was wrong.
至少 this discussion 改变了这一点:
a?.b.c // Do you see the error? ;)
I will get a NullPointerException. I mean, if you use the null-safe ?. operator in a chained expression,
you have to use in all properties because if you forget to put in somewhere, you will get an error too. It will
be nice if Groovy could detect the ?. operator in an expression, and it injects it in the other properties if
it sees the operator is missing. So, if you would type this
a?.b?.e.f?.g // I forget to put the ?. .n the "f" property
a?.b.e.f.g
Groovy could fix it with a real null-safe expression like this:
a?.b?.e?.f?.g
我认为您的假设不正确。这个:
params.stringValue?.trim().replaceAll('aa', 'a')
并不意味着:
if(params.stringValue is null)
dont proceed to trim() and replaceAll()
它的意思是:
if(params.stringValue is null)
skip trim() without complain but pass null to replaceAll()
所以你需要说:
params.stringValue?.trim()?.replaceAll('aa', 'a')
如果传入参数为空,这将跳过 trim()
和 replaceAll()
。
您的假设仅部分正确。
?
-operator 在 null 的情况下不会中断执行,它会阻止调用当前方法,而 returns 会调用 null
,这就是为什么它是必要的还用 ?
保护链的右侧
我的项目有如下代码:
params.stringValue?.trim().replaceAll('aa', 'a')
我们预计如果 params.stringValue
为 null,则 trim()
和 replaceAll()
都不会被调用。
然而我们在这一行得到了一个 NullPointerException
说 replaceAll()
不能在空对象上调用。
我们不得不将代码改成这样:
params.stringValue?.trim()?.replaceAll('aa', 'a')
为什么上面的第一个代码片段不起作用?这是 Groovy 中的一个错误,它会在遇到 null 一次后继续计算表达式吗?
这确实是 Groovy 的工作方式,并且已经被咬 others:
println book?.author?.firstName?.trim().concat(" is great.")
...
Looking at this line of code, I thought for sure I was safe from any sneaky NullPointerException. If book, author or firstName are null I'll simply end up printing null and not have to worry about the concat() method. After all, if the trim() method succeeds, there's no sense in guarding it's result for null. And that's where I was wrong.
至少 this discussion 改变了这一点:
a?.b.c // Do you see the error? ;)
I will get a NullPointerException. I mean, if you use the null-safe ?. operator in a chained expression, you have to use in all properties because if you forget to put in somewhere, you will get an error too. It will be nice if Groovy could detect the ?. operator in an expression, and it injects it in the other properties if it sees the operator is missing. So, if you would type this
a?.b?.e.f?.g // I forget to put the ?. .n the "f" property a?.b.e.f.g
Groovy could fix it with a real null-safe expression like this:
a?.b?.e?.f?.g
我认为您的假设不正确。这个:
params.stringValue?.trim().replaceAll('aa', 'a')
并不意味着:
if(params.stringValue is null)
dont proceed to trim() and replaceAll()
它的意思是:
if(params.stringValue is null)
skip trim() without complain but pass null to replaceAll()
所以你需要说:
params.stringValue?.trim()?.replaceAll('aa', 'a')
如果传入参数为空,这将跳过 trim()
和 replaceAll()
。
您的假设仅部分正确。
?
-operator 在 null 的情况下不会中断执行,它会阻止调用当前方法,而 returns 会调用 null
,这就是为什么它是必要的还用 ?