【问题标题】:Eiffel: non-compatible actual argument in feature call埃菲尔:功能调用中不兼容的实际参数
【发布时间】:2019-02-01 03:33:47
【问题描述】:

我不明白为什么会这样。

  • 如果我说 attached {G} 它有效
  • 如果我什么都不说,这对我来说是调用实体的预期行为:可拆卸的 RELATED_DB_EN​​TITY 应该符合(一旦附加)到 G,即 -> DB_EN​​TITY
  • 如果我说 DB_EN​​TITY 它没有
  • 如果我说 RELATED_DB_EN​​TITY 是否通过

为什么我必须指定{G}???

SIT_HANDLER

class
    SIT_HANDLER[G -> DB_ENTITY create default_create, make_from_db_service, make_from_json end]

feature --

    some_feature
        do
            if attached {G} l_rest_request.calling_entity as l_calling_entity then
                db_service.set_item_prototype (l_calling_entity) -- Complains here!!!!!!!!!!!!
                db_service.load_with_relationships (l_rest_request)
            ...
        end
end -- class

REST_REQUEST

class
    REST_REQUEST

feature -- Access
    calling_entity: detachable RELATED_DB_ENTITY -- RELATED_DB_ENTITY inherits DB_ENTITY
...
end -- class

DB_SERVICE

class
    DB_SERVICE [G -> DB_ENTITY create default_create, make_from_db_service, make_from_json end]

feature -- Status setting

    item_prototype: G

    set_item_prototype (v: like item_prototype)
        do
            item_prototype := v
        ensure
            item_prototype = v
        end

...
end -- class

【问题讨论】:

    标签: polymorphism generic-programming eiffel


    【解决方案1】:

    RELATED_DB_ENTITY 类型不符合G 类型。

    这里是一个例子。假设有一个类FOO 继承自DB_ENTITY 并具有所有必需的创建过程。 FOO 和 RELATED_DB_ENTITY 彼此不一致。对于SIT_HANDLER [FOO] 类型,特征db_service.set_item_prototype 的参数类型为FOO,而表达式l_rest_request.calling_entity 的类型为RELATED_DB_ENTITY。不允许将RELATED_DB_ENTITY 类型的表达式分配给FOO 类型的实体。

    【讨论】:

    • 最后,我要做的是set_item_prototype (some_RELATED_DB_ENTITY) 其中set_item_prototype 需要一个G,它是->DB_EN​​TITY?!!!为什么some_RELATED_DB_ENTITY 继承自DB_ENTITY 不符合?!对不起,但即使读了很多遍我还是不明白。你说 RELATED_DB_EN​​TITY 不符合 G 为什么如果 G -> DB_ENTITY 和 RELATED_DB_ENTITY -> DB_ENTITY?据我了解 DB_EN​​TITY 不符合 RELATED_DB_EN​​TITY 但反过来是的!?!
    • @Pipo RELATED_DB_ENTITY 不符合G 因为RELATED_DB_ENTITY 不符合任意FOO。
    • 当我们有 X -> Z 和 Y -> Z 时,并不意味着 X -> Y。这里我们有:X = G, Y = RELATED_DB_EN​​TITY, Z = DB_EN​​TITY
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-08
    相关资源
    最近更新 更多