eiffel

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]

Solution

  • Conformance to a collection of types is not supported for several reasons: