繁体   English   中英

埃菲尔铁塔:无效安全性,一种简洁的方法来测试对象是否存在,然后调用其功能

[英]Eiffel: void safety, a concise way to test if an object exists and then call its feature

我想知道是否有更清晰的陈述

if not attached foo then
    create foo
end
if attached foo as l_foo then
    l_foo.bark
end

if not attached foo then
    create foo
    foo.bark
else
    foo.bark
end

会重复foo.bark ,显然我想避免它...甚至最后一条语句也不会使用void-safety进行编译,因为其他的foo可能是无效的...

为了避免代码重复和多次测试,可以使用以下代码:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM