显式创建类型不符合目标类型 [英] explicit creation type not conforming to type of target

查看:80
本文介绍了显式创建类型不符合目标类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不理解,因为在我看来,类型一致性的基础知识.我在Eiffel Studio 19.5企业版的create {JANITZA_DEVICE} l_device.make_from_file_path (a_file_path)上有一个Creation instruction lists explicit creation type which does not conform to type of target.

I really don't understand as it seems to me the basics of type conformance. I have a Creation instruction lists explicit creation type which does not conform to type of target on create {JANITZA_DEVICE} l_device.make_from_file_path (a_file_path) with eiffel studio 19.5 enterprise.

class
    SMA_INVERTER_MANAGER_CSV

inherit
    SUNSPEC_DEVICE_CSV[SMA_INVERTER_MANAGER_DEVICE]

create
    make

SUNSPEC_DEVICE_CSV

deferred class
    SUNSPEC_DEVICE_CSV[G -> SUNSPEC_DEVICE create make_from_file_path end]

inherit
    CONSUMPTION_SECTOR_CSV[G]
        redefine
            process_file,
            set_header_csv
        end

feature --

    process_file (a_file_path: PATH)
        require else
            attached a_file_path.entry
            attached consumption_sector
        local
            l_device: like devices.item
        do
            check
                attached_consumption_sector: attached consumption_sector
            then
                if is_valid_file_path (a_file_path) then
                    if attached a_file_path.utf_8_name.has_substring ("janitza_UMG604") then
                        create {JANITZA_DEVICE} l_device.make_from_file_path (a_file_path) -- The compiler doesn't agree!
                    else
                        create l_device.make_from_file_path (a_file_path)
                    end
                    l_device.load_measuring_point (create_measuring_points, measuring_point_db_service, consumption_sector)
                    devices.extend (l_device)
                    Precursor (a_file_path) -- load measure_units from csv_row
                    devices.wipe_out
                end
            end
        ensure then
            devices.is_empty
        end

CONSUMPTION_SECTOR_CSV [G]

deferred class
    CONSUMPTION_SECTOR_CSV[G -> MEASURING_POINT_DEVICE]

feature -- Access

    devices: LINKED_SET[G]

SUNSPEC_DEVICE

class
    SUNSPEC_DEVICE

inherit
    MEASURING_POINT_DEVICE
        redefine
            default_create,
            set_measuring_point,
            out
        select
            serial
        end

    MODBUS_DEVICE
        rename
            serial as modbus_serial,
            set_serial as set_modbus_serial
        undefine
            make
        redefine
            default_create,
            make_from_file_path,
            name_from_file_path,
            out
        select
            set_modbus_serial
        end

create
    make_from_file_path

JANITZA_DEVICE

class
    JANITZA_DEVICE

inherit
    SUNSPEC_DEVICE
        redefine
            set_measure_units,
            name_from_file_path
        end

create
    make_from_file_path

推荐答案

这是一个简化的情况:

class ANIMAL

class CAT inherit ANIMAL

class HOUSE_CAT inherit CAT

class DOG inherit ANIMAL

class
    ENCLOSURE [G -> ANIMAL]
feature
    specimens: LIST [G] --> The actual type will vary with the generic parameter

    describe
        do
            print(specimens.generating_type)
        end

class
    APPLICATION

feature

    test
        local
            l_cat: CAT
            l_animal_enclosure: ENCLOSURE [ANIMAL]
            l_cat_enclosure: ENCLOSURE [CAT]
            l_house_cat_enclosure: ENCLOSURE [HOUSE_CAT]
            l_dog_enclosure: ENCLOSURE [DOG]
        do
            create l_specimen

            create l_animal_enclosure
            l_animal_enclosure.describe --> LIST [ANIMAL]
            l_animal_enclosure.specimens.add (l_cat) --> Fine, CAT conforms to ANIMAL

            create l_cat_enclosure
            l_cat_enclosure.describe --> LIST [CAT]
            l_cat_enclosure.specimens.add (l_cat) --> Fine, CAT conforms to CAT

            create l_house_cat_enclosure
            l_house_cat_enclosure.describe --> LIST [HOUSE_CAT]
            l_house_cat_enclosure.specimens.add (l_cat) --> ERROR, CAT does not conform to HOUSE_CAT


            create l_dog_enclosure
            l_dog_enclosure.describe --> LIST [DOG]
            l_dog_enclosure.specimens.add (l_cat) --> ERROR, CAT does not conform to DOG
        end

在您的情况下,devices: LINKED_SET [G]太模糊,没有任何证据表明JANITZA_DEVICE是有效的类型,因为G可能最终会降低层次结构的下级位置(例如HOUSE_CAT-> CAT;您不能替代) CATHOUSE_CAT,因此HOUSE_CAT的列表不能容纳CAT)或在层次结构的单独分支中(例如DOG-> ANIMAL;狗不是猫,他们只有一个共同的祖先.

In your case, devices: LINKED_SET [G] is too vague, nothing proves that JANITZA_DEVICE is a valid type because G might end up being lower down the hierarchy (like HOUSE_CAT -> CAT; you cannot substitute a CAT to a HOUSE_CAT, thus a list of HOUSE_CAT cannot accommodate a CAT) or in a separate branch of the hierarchy (like DOG -> ANIMAL; a dog is not a cat, they only share a common ancestor).

如果将{ENCLOSURE}.specimens声明为LIST [ANIMAL]而不是LIST [G],则describe将始终打印LIST [ANIMAL]而不管实际的通用参数是什么,因为类型不会改变,因此前面的代码将编译并仅运行美好的. 同样,如果将{CONSUMPTION_SECTOR_CSV}.devices声明为LINKED_SET [SUNSPEC_DEVICE]而不是LINKED_SET [G],则无论G的实际类型如何,它都可以容纳SUNSPEC_DEVICE的所有后代.

If {ENCLOSURE}.specimens was declared as LIST [ANIMAL] instead of LIST [G], describe would always print LIST [ANIMAL] regardless of the actual generic parameter because the type would not vary, thus the previous code would compile and run just fine. Similarly, if {CONSUMPTION_SECTOR_CSV}.devices was declared as LINKED_SET [SUNSPEC_DEVICE] instead of LINKED_SET [G], it could accommodate all descendants of SUNSPEC_DEVICE regardless of the actual type of G.

或者,您可以将特定于JANITZA_DEVICE的零件从CONSUMPTION_SECTOR_CSV移动到CONSUMPTION_SECTOR_CSV的后代,其中G是闭合的,如

Alternatively, you could move the parts specific to JANITZA_DEVICE from CONSUMPTION_SECTOR_CSV to a descendant of CONSUMPTION_SECTOR_CSV where G is closed, as in

class
    JANITZA_CONSUMPTION_SECTOR_CSV -- No generic here!
inherit
    CONSUMPTION_SECTOR_CSV [JANITZA_DEVICE]
        redefine
            process_file -- Add the parts specific to `JANITZA_DEVICE` in the implementation
        end

这将确保devices可以容纳实例JANITZA_DEVICE.

which would ensure that devices can hold instances JANITZA_DEVICE.

这篇关于显式创建类型不符合目标类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆