dao icon indicating copy to clipboard operation
dao copied to clipboard

Weird typing rules for empty containers

Open Night-walker opened this issue 10 years ago • 4 comments

(dao) l: list<int> = {}
= {  }
(dao) l2 = (list<io::Stream>)l
= {  }

That won't work for a non-empty list, so the behavior here seems exceedingly awkward.

Night-walker avatar Jun 16 '15 10:06 Night-walker

The casting on lists (and maps) is done in element-wise, so you can cast empty list to any other type of list.

daokoder avatar Jun 18 '15 14:06 daokoder

That's so confusing that it arguably brings more harm then help:

(dao) routine cast(x: list<io::Stream>){
..... io.writeln((list<int>)x)
..... }
= none
(dao) cast({})
{  }
= none
(dao) cast({io::stdio})
{ 0 }
= none
(dao) (int)io::stdio
[[Error::Type]] --- Invalid type:
casting from 'Stream' to 'int'.
In code snippet:
      0 :  GETCL       :     0 ,     0 ,     0 ;     1;   io
>>    1 :  CAST_I      :     0 ,     1 ,     1 ;     1;   (int)io::stdio
      2 :  RETURN      :     1 ,     1 ,     0 ;     1;   (int)io::stdio
Raised by:  __main__(), at instruction 1 in line 1 in file "interactive codes";

list<io::Stream> can be cast to list<int>, while io::Stream to int -- cannot? The logic here is puzzling.

Night-walker avatar Jun 18 '15 15:06 Night-walker

That's not all.

(dao) class A {}
= none
(dao) interface I{}
= none
(dao) a = A()
= A[02544460]
(dao) (I)a
= A[02544460]
(dao) l = {a}
= { A[02544460] }
(dao) (list<I>)l
[[Error::Type]] --- Invalid type:
casting from 'list<A>' to 'list<I>'.
In code snippet:
      0 :  GETVG       :     0 ,     3 ,     0 ;     1;   l
>>    1 :  CAST        :     0 ,     0 ,     1 ;     1;   (list<I>)l
      2 :  RETURN      :     1 ,     1 ,     0 ;     1;   (list<I>)l
Raised by:  __main__(), at instruction 1 in line 1 in file "interactive codes";

Now it's immensely confusing for me.

Night-walker avatar Jun 18 '15 15:06 Night-walker

OK, now there are bugs.

daokoder avatar Jun 18 '15 15:06 daokoder