1

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

jjj
  • 23
  • 8

1 Answers1

1

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])
user7860670
  • 35,849
  • 4
  • 58
  • 84
  • Okay. But why then can I use " feature make (a_tuple: TUPLE [INTEGER])" and call it with "create my_object.make ([1, 2, 3])"? – jjj Mar 16 '23 at 16:32
  • @jjj Because `TUPLE [INTEGER, INTEGER, INTEGER]` is an also an instance of `TUPLE [INTEGER, INTEGER]` and `TUPLE [INTEGER]` (and just `TUPLE`) – user7860670 Mar 16 '23 at 16:53
  • So that conformaty applies to assignments and argument passing but not conversions? – jjj Mar 16 '23 at 18:07
  • @jjj Eiffel does not allow feature overloading (e.g. declaring `feature from_tuple (a_shape: TUPLE [INTEGER]) ... feature from_tuple (a_shape: TUPLE [INTEGER, INTEGER]) ... `). However conversion operation may have several overloads and the exact match will be selected. – user7860670 Mar 16 '23 at 18:53
  • So the conversion feature `from_tuple (a_tuple: INTEGER)' could accept a TUPLE [INTEGER, INTEGER, ...] as argument because of conformity rules, but using that feature as a conversion routine require exact match? – jjj Mar 16 '23 at 20:36
  • @jjj Yes, looks like that. – user7860670 Mar 16 '23 at 20:55