[英]Eiffel: void safety, a concise way to test if an object exists and then call its feature
I was wondering if there is a clearer statement then 我想知道是否有更清晰的陈述
if not attached foo then
create foo
end
if attached foo as l_foo then
l_foo.bark
end
as 如
if not attached foo then
create foo
foo.bark
else
foo.bark
end
would repeat the foo.bark
and obviously I want to avoid it... and even the last statement won't compile with void-safety as foo on else could be void... 会重复
foo.bark
,显然我想避免它...甚至最后一条语句也不会使用void-safety进行编译,因为其他的foo可能是无效的...
To avoid code duplication and multiple tests, the following code could be used: 为了避免代码重复和多次测试,可以使用以下代码:
l_foo := foo
if not attached l_foo then
create l_foo
foo := l_foo
end
l_foo.bark
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.