埃菲尔铁塔:或和中的本地声明和编译失败

时间:2018-10-08 20:51:38

标签: compiler-errors eiffel

编译器抱怨一个未知的标识符,似乎它无法识别我的多个声明中的任何一个,我在哪里错了?

if attached {INTEGER_REF} field.item as l_int
        or attached {INTEGER_64} field.item as l_int
        or ( attached {TUPLE} field.item as l_tuple and then attached {INTEGER_64} l_tuple.item (1) as l_int ) then
    Result.put_integer (l_int.to_integer_64, primary_key_db_column_name)
elseif attached {STRING} field.item as l_s then
    Result.put_string (l_s, primary_key_db_column_name)
end

enter image description here

更新

因为这似乎是一个有效的表达,所以我认为,如果在我的的每个分支中都声明了l_int,我应该可以在然后范围。

但是这个表达式是有效的

if attached a.b as l_b and then attached l_b.c as l_c then
    l_c.is_available_in_this_scope
    l_b.is_available_in_this_scope
else
    io.putstring ("you are wrong dear and either l_b and l_c are not available!")
end

这是不是!

if attached a.b as l_b and then attached l_b.c as l_c
        or attached a.x as l_b and then attached l_x.d as l_c then
    l_c.is_available_in_this_scope -- Compiler complain about l_c
    l_b.is_available_in_this_scope -- Compiler complain about l_b
else
    io.putstring ("you are wrong dear and either l_b and l_c are not available!")
end

使用我的代码

编译

    if attached {INTEGER_REF} field.item as l_int then
        Result.put_integer (l_int.to_integer_64, primary_key_db_column_name)
    elseif attached {INTEGER_64} field.item as l_int then
        Result.put_integer (l_int, primary_key_db_column_name)
    elseif attached {TUPLE} field.item as l_tuple and then attached {INTEGER_64} l_tuple.item (1) as l_int then
        Result.put_integer (l_int, primary_key_db_column_name)
    elseif attached {STRING} field.item as l_s then
        Result.put_string (l_s, primary_key_db_column_name)
    else
        logger.write_error ("to_json-> Type not found in matching:" + field.item.out)
        check
            not_found_item_type: False
        end
    end

不是

if attached {INTEGER_REF} field.item as l_int then
    Result.put_integer (l_int.to_integer_64, primary_key_db_column_name)
elseif attached {INTEGER_64} field.item as l_int
        or attached {TUPLE} field.item as l_tuple and then attached {INTEGER_64} l_tuple.item (1) as l_int then
    Result.put_integer (l_int, primary_key_db_column_name) -- Unknown identifier `l_int`
elseif attached {STRING} field.item as l_s then
    Result.put_string (l_s, primary_key_db_column_name)
else
    logger.write_error ("to_json-> Type not found in matching:" + field.item.out)
    check
        not_found_item_type: False
    end
end

2 个答案:

答案 0 :(得分:1)

对象测试具有范围。用OT表示对象测试,其范围为

if OT         then A else B end
if OT and ... then A else B end
if OT or  ... then C else B end

仅为A。因此,对于析取而言,作用域为空,并且您不能在任何分支中使用相应的局部对象测试。

如果条件中有两个对象测试,则它们的范围可能重叠或不重叠:

if OT1 and      OT2 then A else B end
if OT1 and then OT2 then A else B end
if OT1 or       OT2 then C else B end

在这里,像以前一样,对象测试局部变量OT1的作用域为A。另外,对于and then,范围包括OT2,特别是OT2可以使用OT1的本地。出于相同的原因,OT2无法使用OT1的同一对象测试本地。

为了区分,OT1OT2的对象测试局部变量的范围为空。为了提供更多信息,带有助记符名称的相同代码如下:

if attached e1 as x and      attached e2_without_x as y then use_x_and_y else B end
if attached e1 as x and then attached e2_with_x    as y then use_x_and_y else B end
if attached e1 as x or       attached e2_without_x as y then no_x_no_y   else B end

如果所有涉及的表达式的类型都相同,则仍然可以只用一个第一分支来重写示例。(不是这种情况,因为存在类型{{1} }和INTEGER_64):

INTEGER_REF

但是这变得太麻烦了,使用多个分支或临时局部变量看起来是更好的选择。

答案 1 :(得分:0)

首先,您不能对同一作用域中的多个对象测试本地使用相同的标识符(l_int)。