简体   繁体   English

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

[英]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.

相关问题 Eiffel:一种在另一个函数上调用前体重新定义的功能的方法 - Eiffel: a way to call precursor redefined feature on another function 埃菲尔铁塔:有没有办法在埃菲尔铁塔上表达双重隐含条款? - Eiffel: is there a way to express a double implies clause in eiffel? 使用EIFFEL上的项目功能在HASH_TABLE中查找对象 - Finding a object in a HASH_TABLE using item feature on EIFFEL 带有额外参数的Eiffel sqlite通话 - Eiffel sqlite call with extra parameters 埃菲尔:有没有一种方法可以在没有任何附加实例的情况下测试类的给定泛型参数? - Eiffel: Is there a way to test a given Generic parameter of a Class without any attached instance of it? 埃菲尔铁塔:如果找不到对象,使用吸气剂的最佳做法将返回Void? - Eiffel: Best practices with getters return Void if object not found? 埃菲尔铁塔:功能调用中的实际参数不兼容 - Eiffel: non-compatible actual argument in feature call 如何在Eiffel的声明中初始化局部变量? - How to initialize a local variable in its declaration in Eiffel? Eiffel:我如何进行系统调用? - Eiffel: how do I do a system call? 埃菲尔:呼叫使用已过时的功能。 调用功能“ to_string_8”:改用“ name_32” - Eiffel: Call use obsolete feature. Call to feature `to_string_8': Use `name_32' instead
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM