twinbasic / lang-design

Language Design for twinBASIC
MIT License
11 stars 1 forks source link

Strongly typed Arrays / Tuples / UDT Destructuring #66

Open Greedquest opened 7 months ago

Greedquest commented 7 months ago

Related #28 & #14

Dim As Long a, b

(a , b) = foo()  'something like that

Function foo() As (Long, Long) 'maybe...

Motivation

VBA has User Defined Types, Strongly typed arrays, and variant arrays. All of these should support destructuring into variables (a,b,c = someValues. What's more, there should be compile time type inference and run time type coercion semantics. Right now only variant arrays can be destructured and you lose all the type info, both for intellisense and for casting from one type to another. They are also better for performance than relying on variant arrays citation needed

Implementation

One suggestion has been to add tuples which would be a new type primitive that can be parameterised by other types (e.g. a tuple of longs, a tuple of Foos). Named Tuples are very similar and provide dot.access to their members - like a Point2D has a .x and a .y but unlike a class, can also be destructured as x, y = getPoint2D(). To be honest if user defined types could be destructured and indexed into like arrays, then they would eliminate the need for named tuples.

As has been mentioned, this would solve the multiple function return problem in a type safe way.

Considerations

Representation in a compiled COM dll

Greedquest commented 7 months ago

Possible v1 blocker Purely if we come up with something that steps on the toes of the new destructuring assignment syntax and want to retire that in favour of whatever this becomes

WaynePhillipsEA commented 7 months ago

I don't think this one would be considered a blocker. If we did retire it completely, it would just be a case of the developer needing to copy the property-let procedure implementation over from the current VBA library to regain the functionality.