[英]Eiffel: Creator instruction applies to target of a deferred type
deferred Class A
feature --
item: X -- X is deferred
set_item_for_some_reason (param: N)
do
create item.make_from_param (param)
end
end -- class
Class B inherit
A
feature --
item: Y -- Y not deferred inherits from X
end -- class
I'd like to create in the same class an attribute which will be defined in the descendent and get a Creator instruction applies to target of a deferred type
Error which makes sens in a way of reduced context, but not in the context I intend it to do it. 我想在同一个类中创建一个属性,该属性将在后代中定义,并获得一个
Creator instruction applies to target of a deferred type
Error的Creator instruction applies to target of a deferred type
错误以减少上下文的方式产生感觉,但在我不希望使用的上下文中去做吧。
For me it does make sense to be able to create an object in current deferred class, I wont have to implement in all the descendants! 对我而言,能够在当前的延迟类中创建对象确实很有意义,我将不必在所有后代中实现! which will be a wrong design... something like this:
这将是一个错误的设计...类似这样的东西:
deferred Class A
feature --
item: X -- X is deferred
set_item_for_some_reason (param: N)
do
set_item_from_param (param)
end
set_item_from_param (param: N)
deferred
end
end -- class
Class B inherit
A
feature --
item: Y -- Y not deferred
set_item_from_param(param: N)
do
create item.make_from_param (param)
end
end -- class
Am I wrong with my design or is it a restriction which is still discussed about Eiffel compiler as I understood ? 我的设计是否错了?还是我所了解的有关Eiffel编译器的限制? and if yes, what would be the best practice workaround?
如果是的话,最佳做法是什么?
A possible solution is to use generic classes. 一种可能的解决方案是使用泛型类。 In the class
A
, the formal generic parameter has a creation constraint that the corresponding actual generic parameter should have a particular creation procedure: 在类
A
,形式通用参数具有创建约束,即相应的实际通用参数应具有特定的创建过程:
class A [G -> X create make_from_param end] feature
item: detachable G
set_item_for_some_reason (param: N)
do
create item.make_from_param (param)
end
end
A descendant class can specify an actual generic that has this creation procedure: 后代类可以指定具有以下创建过程的实际泛型:
class B inherit
A [Y]
end
To make sure we are on the same page, here is the code of classes X
and Y
: 为了确保我们在同一页上,这是
X
和Y
类的代码:
deferred class X feature
make_from_param (param: N)
deferred
end
end
class Y inherit
X
create
make_from_param
feature
make_from_param (param: N)
do
end
end
You can add as many such descendants as you wish. 您可以根据需要添加任意多个后代。 The main restriction is that whenever class
A
is used, its actual generic parameter should have the specified feature as a creation one. 主要限制是,每当使用类
A
,其实际通用参数都应具有指定的功能作为创建功能。 For example, it's OK to declare a type A [Y]
. 例如,可以声明类型
A [Y]
。 But A [X]
would trigger an error. 但是
A [X]
会触发错误。
If in a descendant, the type of item
should not be fixed yet, it's possible to propagate it, repeating the constraint on the formal generic: 如果在子的类型,
item
不应该未定,有可能传播它,重复的正式通用的约束:
class C [G -> X create make_from_param end] inherit
A [G]
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.