-
-
Notifications
You must be signed in to change notification settings - Fork 665
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal for struct-like datatypes #2858
Comments
For nested structs, something like this :
Could become :
Makes my head spin a little, might have made some mistakes, so consider this pseudo-code, and I hope the concept is clear. Edit : it probably shouldn't call constructors for getting the encapsulated "structs", and rather do the memory stuff directly, because we wouldn't want to call the constructor every time a struct gets copied. |
One caveat is that there is no stack in AS, except for the shadow stack used for garbage collection. |
@CountBleck, I suppose you could allocate a page or two and call that the stack like https://github.com/fabricio-p/as-malloc does |
I believe this was discussed elsewhere and a long while back, but multi-value would probably be better suited for AS. Binaryen implements multi-value using a special tuple type, so I believe you can have tuples as local variables (that get exploded into their constituent variables). That likely won't solve this use case though, since it would be pass-by-value and not pass-by-reference, and I'm not sure whether nesting tuples is supported at all. Another good fit would be GC types, which are pass-by-reference and support nesting...but they can't be stored to regular classes since they're opaque. @JairusSW you could definitely use |
Wait, that's confusing me - then what is __stack_pointer for? |
|
Oh ok, it's slowly starting to make sense. Correct me if I'm wrong, but for the usecase I showed, I think this wouldn't actually be a problem since the constructor is inlined, so the "reinterpreted" Vector2 it's returning will still be unique. My main reason for wanting something like this (besides GC / performance) is that without value types, doing Tuples / multi values sounds like it would work, but I'm unclear on whether that's implemented in AssemblyScript at this point, or if it's planned? |
Oh I see the flaw with this approach. If I would call the inlined constructor in a loop, and add them to an array, they will all be the same references / pointers. Unless of course that would get transformed too, but yeah, complexity adds up quickly. Guess I’ll just wait for tuple support. |
Feature suggestion
Hi,
I had this idea for C#-style structs that I believe could be implemented using a Transform.
I know structs have been brought up a couple of times, so there's clearly some demand.
Take this code :
That could be transformed into this :
Which essentially makes the Vector2 class allocate on the stack when instantiated, while still being able to assign it to objects.
Obviously it gets more complex with nested structs and generics involved, and things like offsetof("position") would also need to be transformed, and respecting the original's constructor code would become tricky.
Also, I believe it would need to turn things like this :
Into something like this :
Not entirely sure how to deal with passing it around as a parameter or returning it, because my understanding of how memory works in AssemblyScript is a bit limited.
Would love to hear some thoughts on this idea.
The text was updated successfully, but these errors were encountered: