简体   繁体   English

有没有办法为参数指定各种类型

[英]Is there a way to specify various types for a parameter

Is there a way to restrict the conformance of a type to be a collection of types?有没有办法将类型的一致性限制为类型的集合?

Let me explain by example:让我举例说明:

give_foo (garbage: ANY): STRING
    do
        if attached {STRING} garbage as l_s then
            Result := l_s
        elseif attached {INTEGER} garbage as l_int then
            Result := l_int.out
        elseif attached {JSON_OBJECT} garbage as l_int then
            Result := l_int.representation
        elseif attached {RABBIT} garbage as l_animal then
            Result := l_animal.name + l_animal.color
        else
            Result := ""
            check
                unchecked_type_that_compiler_should_be_able_to_check_for_me: False
            end
        end
    end

Couldn't I do something like (like a convert function could do)我不能做类似的事情(就像转换函数可以做的那样)

give_foo (garbage: {STRING, INTEGER, JSON_OBJECT, RABBIT}): STRING
    do
        if attached {STRING} garbage as l_s then
            Result := l_s
        elseif attached {INTEGER} garbage as l_int then
            Result := l_int.out
        elseif attached {JSON_OBJECT} garbage as l_int then
            Result := l_int.representation
        elseif attached {RABBIT} garbage as l_animal then
            Result := l_animal.name + l_animal.color
        else
            Result := ""
            check
                unchecked_type_that_compiler_should_be_able_to_check_for_me: False
            end
        end
    end

or something like或类似的东西

not_garbage_hash_table: HASH_TABLE[{INTEGER, STRING, ANIMAL}, STRING]

Conformance to a collection of types is not supported for several reasons:由于以下几个原因,不支持符合类型集合:

  • Calling a feature on an expression of such a type becomes ambiguous because the same name could refer to completely unrelated features.在这种类型的表达式上调用特征变得不明确,因为相同的名称可能指代完全不相关的特征。
  • In one case we need a sum (disjoint union) of types, in the second - plain union, in the third - an intersection, etc. And then, there could be combinations.在一种情况下,我们需要类型的总和(不相交联合),在第二个 - 普通联合中,在第三个 - 交集等。然后,可能会有组合。 One would need an algebra built on top of a type system that becomes too complicated.人们需要一个建立在一个变得过于复杂的类型系统之上的代数。
  • If the requirement is to check that an argument is one of expected types, the following precondition can be used:如果需要检查参数是否为预期类型之一,则可以使用以下前提条件:

     across {ARRAY [TYPE [detachable ANY]]} <<{detachable STRING}, {INTEGER}, {detachable JSON_OBJECT}>> as t some argument.generating_type.conforms_to (t.item) end
  • A common practice to process an expression of a potentially unknown type is a visitor pattern that deals with known cases and falls back to a default for unknown ones.处理潜在未知类型表达式的常见做法是访问者模式,该模式处理已知情况并回退到未知情况的默认值。

Possibly place Alexander's solution in a BOOLEAN query so it can be reused?可能将亚历山大的解决方案放在 BOOLEAN 查询中,以便可以重用?

is_string_integer_or_json_object (v: detachable ANY): BOOLEAN
         -- Does `v' conform to {STRING}, {INTEGER}, or {JSON_OBJECT}?
    do
       across {ARRAY [TYPE [detachable ANY]]}
        <<{detachable STRING}, {INTEGER}, {detachable JSON_OBJECT}>> as t
       some v.generating_type.conforms_to (t.item) end
    end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 埃菲尔铁塔,多种类型的一致性:一种指定参数是A和B的后代的方法吗? - Eiffel, multiple types conformance: a way to specify that a parameter is a descendent from A and B? 如何在给出一个泛型参数时指定它应该实现一些特定的创建方法? - How to specify when giving a generic parameter that it should implement some specific creation method? 埃菲尔:有没有一种方法可以在没有任何附加实例的情况下测试类的给定泛型参数? - Eiffel: Is there a way to test a given Generic parameter of a Class without any attached instance of it? 作为Haskell中扩展类的原始类型? - Primitive types as expanded classes in Haskell? 埃菲尔铁塔:比较类型的最佳方法而不会引起注意 - Eiffel: best way to compare types without getting a catcall 埃菲尔铁塔:有没有办法在埃菲尔铁塔上表达双重隐含条款? - Eiffel: is there a way to express a double implies clause in eiffel? 埃菲尔铁塔:创建可重新定义的“常数”的最佳方法 - Eiffel: best way to create a redefinable “constant” Eiffel:一种在另一个函数上调用前体重新定义的功能的方法 - Eiffel: a way to call precursor redefined feature on another function 埃菲尔:协变非法类型作为参数传递? - Eiffel: Covariant illegal types passed as arguments? 埃菲尔:扩展类型符合ANY吗? - Eiffel: Do expanded types conform to ANY?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM