How to use convert for creation

79 views Asked by At

Using "Void safety - complete", my code:

class TENSOR_SHAPE
create
    default_create,
    from_tuple
convert
    from_tuple ({TUPLE [INTEGER]})
feature -- Initialization
    from_tuple (a_shape: TUPLE [INTEGER])
...

I try to call this with:

    local
        s: TENSOR_SHAPE
    do
        s := [2, 4, 3, 5]  -- convert from tuple?

I get "source of assignment is not compatible with target."

    Target name:  s
    Target type:  detachable TENSOR_SHAPE
    Source type:  attached TUPLE [INTEGER_32, ...]
on line "s := [2, 4, 3, 5]".

Why does the compiler say `s' is detachable? Is this not how conversions are used? thanks, jjj

1

There are 1 answers

6
user7860670 On

TUPLE [INTEGER] convert declaration will be selected when argument is a tuple containing a single INTEGER, for example:

s := [2]

Now in order to handle [2, 4, 3, 5] a matching declaration is required:

convert
    from_tuple ({TUPLE [INTEGER, INTEGER, INTEGER, INTEGER]})
feature -- Initialization
    from_tuple (a_shape: TUPLE [INTEGER, INTEGER, INTEGER, INTEGER])

Also an interesting moment here is that feature may actually accept a tuple subtype with smaller amount of items, so the following example will also work:

convert
    from_tuple ({TUPLE [INTEGER, INTEGER, INTEGER, INTEGER]})
feature -- Initialization
    from_tuple (a_shape: TUPLE [INTEGER])