编译器提示未知标识符,似乎它无法识别我的多个声明中的任何一个,我错在哪里?
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
更新
由于这似乎是一个有效的表达,我认为如果在我的 的每个分支中或 一个
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
最佳答案
对象测试有范围。用 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
的相同对象测试本地.对于析取,
OT1
的对象测试局部变量的范围和 OT2
是空的。为了提供更多信息,具有助记符名称的相同代码如下所示: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
仍然可以仅使用第一个分支 重写示例如果所有涉及的表达式的类型相同 (事实并非如此,因为有类型
INTEGER_64
和 INTEGER_REF
):if attached
if attached {INTEGER_64_REF} field.item as i then
i
elseif
attached {TUPLE} field.item as t and then
t.count > 0 and then
attached {INTEGER_64_REF} t.item (1) as i
then
i
else
Void
end
as j
then
-- Use j
...
但这变得太麻烦了,使用多个分支或临时局部变量看起来是一个更好的选择。
关于compiler-errors - Eiffel :or and and and 中的本地声明编译失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52709935/