簡體   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