daokoder / dao

Dao Programming Language
http://daoscript.org
Other
199 stars 19 forks source link

Invalid VM instruction when setting routine as part of a tuple. #545

Open IngwiePhoenix opened 5 years ago

IngwiePhoenix commented 5 years ago
(dao) tup2 = ( cb = routine() {}, 'abc' )
[[ERROR]] in file "interactive codes":
  At line 0 : Invalid function definition --- " __main__() ";
  At line 1 : Invalid virtual machine instruction --- " SETVG:4,2,0 ";
In code snippet:
      4 :  TUPLE       :     2 ,     2 ,     4 ;     1;   ( cb = routine() {}, 'ab...'...
>>    5 :  SETVG       :     4 ,     2 ,     0 ;     1;   tup2
      6 :  RETURN      :     4 ,     1 ,     0 ;     1;   )
  At line 1 : Invalid operation on the type --- " tup2 ";
  At line 1 : Types not matching --- " 'tuple<cb:routine<=>none>,string>' for 'tuple<index:int,string>' ";

I just tried this out for fun. Well, this is what comes out.

dumblob commented 5 years ago

This is clearly a type error - last line says it explicitly. tup2 was apparently already initialized (last line says that the original type was tuple<index:int,string>, i.e. something like var tup2 = ( index=5, "str00" ).

By default in Dao once variable has a type, you can't change the type any more. If you wanted to change the type (or part of it), you would need to use explicit runtime (i.e. dynamic) typing for which the word any is reserved - e.g. var x: any = (0, "a"); x = (routine(){}, "b") or better (safer) var x: tuple<any, string> = (0, "a"); x = (routine(){}, "b"). But beware, runtime typing can't be checked in advance (in compile time), so it opens another pandora box of programming mistakes and it's also slower than static typing :wink:.

By the way make use of invar instead of var (especially in all interfaces) as it helps to reveal not negligible amount of subtle bugs and has a noticeable positive impact on the programming style :wink: (among other things using read-only or copy-on-write views when interfacing with non-Dao libraries and code allows for significant safety and usually also performance gains).

Hope this clarifies the typing in Dao a bit.