[英]Eiffel: compilation error `Source of assignment is not compatible with target`
With complete void check set in compiler I've got a Variable is not properly set compilation error on following case which for me is right (in my mind). 在编译器中设置了完全无效检查后,在以下情况下,我得到了一个变量未正确设置编译错误,这对我来说是对的(在我看来)。 It says that the source of assignment is not compatible with target. 它说分配的来源与目标不兼容。 What am I missing here??? 我在这里想念什么??? ( DB_SERVICE.load_from_primary_key...
) ( DB_SERVICE.load_from_primary_key...
)
deferred class
DB_SERVICE [G -> DB_ENTITY create make_from_db_result end]
inherit
ACTION
redefine
start,
execute
end
LOGGABLE
rename
make as make_from_loggable
end
feature -- Creation
make (a_db_connection: attached DB_CONNECTION)
require
valid_db_connection: a_db_connection.is_connected
do
make_from_loggable
db_connection := a_db_connection
create last_items.make (100)
create last_column_names.make_empty
ensure
db_connection_setted: a_db_connection = db_connection and db_connection.is_connected
end
feature -- Access
item: detachable G
db_connection: DB_CONNECTION
last_items: HASH_TABLE[like item, INTEGER] -- content of last resultset
last_column_names: ARRAY[STRING] -- Column names of last resultset
feature -- status_report
load_from_primary_key (primary_key: INTEGER)
-- Loads given item into item otherwise item will be Void
require
attached db_connection.base_selection
local
l_db_result: DB_RESULT
do
if attached db_connection.base_selection as bs then
bs.query ("SELECT * FROM " + ({attached like item}).out + " WHERE " + {attached like item}.Primary_key_db_column_name + "=" + primary_key.out)
if bs.is_ok then
bs.load_result
create item.make_from_db_result(last_column_names, bs.cursor)
else
item := Void --HERE is the compiler complaining!
logger.write_critical ("Error while retreiving " + ({like item}).out + " from DB")
end
else
item := Void
logger.write_error ("base_selection is void")
end
end
end -- class
class
COMPANY_SERVICE
inherit
DB_SERVICE[COMPANY]
redefine
make
end
...
class
COMPANY
inherit
DB_ENTITY
rename
primary_key as id,
set_primary_key as set_id,
Primary_key_db_column_name as Id_db_column_name
redefine
make,
make_from_db_result,
out
end
create
make,
make_from_db_result
....
The type declaration detachable G
indicates that if the corresponding actual generic is a reference type, the variable of that type could be detachable. 可声明的类型声明detachable G
表示,如果相应的实际泛型是引用类型,则该类型的变量可能是可分离的。 In that case it would be OK to assign Void
to such a variable. 在这种情况下,可以将Void
分配给此类变量。 However, it's also possible that the actual generic is an expanded type. 但是,实际的泛型也有可能是扩展类型。 Prefixing an expanded type with detachable
has no effect, the type remains the same and the variable cannot be assigned Void
. 给可扩展类型加上detachable
前缀是无效的,该类型保持不变,并且不能为该变量分配Void
。
As an example, let's consider a simpler case, when there is no formal generic constraint for the parameter G
. 例如,让我们考虑一个更简单的情况,当参数G
没有形式通用约束时。 An actual generic could be STRING
, and the variable item
has a type detachable STRING
. 实际的泛型可能是STRING
,而变量item
具有detachable STRING
类型。 In this case, it's OK to assign Void
to item
. 在这种情况下,可以将Void
分配给item
。
Now, if the actual generic is INTEGER
, the variable has a type detachable INTEGER
that is equivalent to INTEGER
. 现在,如果实际的泛型是INTEGER
,则该变量具有detachable INTEGER
类型,该类型与INTEGER
等效。 Assigning Void
to a variable of this type makes no sense and is not permitted by the language rules. 将Void
分配给此类型的变量没有任何意义,并且语言规则不允许这样做。
The variable item
still can be set to Void
if the actual generic parameter is a reference type. 如果实际的通用参数是引用类型,则仍可以将变量item
设置为Void
。 For that, a local variable with the same type can be declared and its value can be assigned to item
: 为此,可以声明具有相同类型的局部变量,并将其值分配给item
:
local
default_item: like item
do
item := default_item
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.