【问题标题】:Eiffel: type casting operators whats the difference between ~ / and attached statements?Eiffel:类型转换运算符 ~ / 和附加语句有什么区别?
【发布时间】:2018-10-31 12:04:29
【问题描述】:

有什么区别

  • 对象测试

    if attached {DOG} an_animal as a_dog then
       a_dog.eat (meat)
    end
    
  • TYPE类的运算符/

    if an_animal / a_dog then
       an_animal.eat (food)
    end
    
  • 引用相等=

    if a_dog = an_animal then
        a_dog.eat (meat)
    else
        an_animal.eat (food) 
    end
    
  • 对象相等~

    if a_dog ~ an_animal then
        a_dog.eat (meat)
    else
        an_animal.eat (food) 
    end
    

我在哪里可以找到相关文档?

【问题讨论】:

    标签: eiffel


    【解决方案1】:

    构造之间的主要区别在于操作数类型和语义。

    1. 对象测试允许确定特定表达式的计算结果是否符合特定类型的值。可以通过关联的对象测试本地检索符合对象的值。

    2. TYPE 类的运算符/ 返回传递参数的值,如果它符合类型对象指定的类型。否则,它为引用类型返回Void,为扩展类型返回默认值。这与对象测试非常相似,但有细微差别。本质上,表达式{SOME_TYPE} / expression 等价于

      if attached {SOME_TYPE} expression as value then
          value
      else
          {detachable SOME_TYPE}.default
      end
      

      对于引用类型,对象测试attached {SOME_TYPE} expression as value等价于

      attached ({SOME_TYPE} / expression) as value
      

      但扩展类型没有等价物。

      运算符/的主要用途是尽可能获取特定类型的值,否则获取Void

      x := {SOME_TYPE} / expression
      
    3. 引用相等= 比较(大部分时间)对象引用,与它们的类型无关。换句话说,引用对象的a = b 意味着ab 是别名。如果其中一个操作数是扩展对象,= 与对象相等性相同(见下文)。

      如果表达式dog = animal 返回True,则变量doganimal 引用同一个对象,但我们不知道它是什么类型。

    4. 对象相等~ 比较两个对象的内容。首先,它检查两者都是非空的,具有相同的类型,然后调用(用户定义的)谓词is_equal 来获取运算符的结果。

      如果表达式dog ~ animal 返回True,则变量doganimal 可以是相同或不同的具有相同类型且相等的对象(根据is_equal)。与前一种情况一样,我们不知道它们是什么类型。

    1、3 和 4 记录在语言标准中,2 是 TYPE 类的特性(对应名称为 attempted)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-02
      • 1970-01-01
      • 2022-06-10
      • 2013-12-27
      • 1970-01-01
      • 2015-06-30
      • 1970-01-01
      • 2018-08-16
      相关资源
      最近更新 更多