简体   繁体   中英

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...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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