代码之家  ›  专栏  ›  技术社区  ›  Pipo

艾菲尔:void-safety,一种测试对象是否存在并调用其特征的简洁方法

  •  0
  • Pipo  · 技术社区  · 6 年前

    我想知道当时是否有更明确的声明

    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

    1 回复  |  直到 6 年前
        1
  •  2
  •   Alexander Kogtenkov    6 年前

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

    l_foo := foo
    if not attached l_foo then
        create l_foo
        foo := l_foo
    end
    l_foo.bark
    
    推荐文章