Eiffel:编译错误“分配源与目标不兼容”
Eiffel: compilation error `Source of assignment is not compatible with target`
在编译器中设置了完整的无效检查后,我在以下情况下遇到了 Variable is not properly set 编译错误,这对我来说是正确的(在我看来)。它说赋值源与目标不兼容。我在这里错过了什么??? (DB_SERVICE.load_from_primary_key...
)
Class DB_SERVICE
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
class
COMPANY_SERVICE
inherit
DB_SERVICE[COMPANY]
redefine
make
end
...
Class 公司
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
....
类型声明detachable G
表示如果相应的实际泛型是引用类型,则该类型的变量可以是可分离的。在那种情况下,可以将 Void
分配给这样的变量。但是,实际泛型也有可能是扩展类型。用 detachable
作为扩展类型的前缀没有效果,类型保持不变并且不能为变量赋值 Void
.
例如,让我们考虑一个更简单的情况,当参数 G
没有正式的通用约束时。一个实际的泛型可能是 STRING
,而变量 item
的类型是 detachable STRING
。这样的话,把Void
赋值给item
.
就可以了
现在,如果实际泛型是 INTEGER
,则变量的类型 detachable INTEGER
等同于 INTEGER
。将 Void
赋值给这种类型的变量是没有意义的,也是语言规则不允许的。
如果实际泛型参数是引用类型,变量item
仍然可以设置为Void
。为此,可以声明一个具有相同类型的局部变量,并将其值分配给 item
:
local
default_item: like item
do
item := default_item
在编译器中设置了完整的无效检查后,我在以下情况下遇到了 Variable is not properly set 编译错误,这对我来说是正确的(在我看来)。它说赋值源与目标不兼容。我在这里错过了什么??? (DB_SERVICE.load_from_primary_key...
)
Class DB_SERVICE
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
class
COMPANY_SERVICE
inherit
DB_SERVICE[COMPANY]
redefine
make
end
...
Class 公司
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
....
类型声明detachable G
表示如果相应的实际泛型是引用类型,则该类型的变量可以是可分离的。在那种情况下,可以将 Void
分配给这样的变量。但是,实际泛型也有可能是扩展类型。用 detachable
作为扩展类型的前缀没有效果,类型保持不变并且不能为变量赋值 Void
.
例如,让我们考虑一个更简单的情况,当参数 G
没有正式的通用约束时。一个实际的泛型可能是 STRING
,而变量 item
的类型是 detachable STRING
。这样的话,把Void
赋值给item
.
现在,如果实际泛型是 INTEGER
,则变量的类型 detachable INTEGER
等同于 INTEGER
。将 Void
赋值给这种类型的变量是没有意义的,也是语言规则不允许的。
如果实际泛型参数是引用类型,变量item
仍然可以设置为Void
。为此,可以声明一个具有相同类型的局部变量,并将其值分配给 item
:
local
default_item: like item
do
item := default_item