Open k06a opened 2 years ago
The main problem I see is that tuples are not "proper" types in solidity.
But it's just the fact that we have no tuple-type variables, right? I don't think this particular feature would force us in any way to introduce them.
Though I'm not sure I like the proposed syntax. The index looks like a field name, which it is not. Why not array brackets instead?
The point is that we do not want to have arbitrary access to tuple elements - the element always has to be a compile-time constant, otherwise code generation gets very complicated.
Yeah, conceptually, as @k06a says, tuples should work like anonymous structs with only compile-time fixed members for accessing tuple elements. Dynamically accessing tuple elements is not only complicated in code generation, but more importantly, it's impossible to type-check, so that's a no-go, but static access is fine.
But yeah, I'd actually even be down for promoting tuples to proper, even namable types in general - I've always wanted that :-). But even without that, compile-time fixed index access to them would definitely be nice - just not sure about the syntax.
@cameel array brackets makes me think elements are of the same type, which is not true.
Also I remember someone asked for ability to use tuples as arguments in function calls:
token.transfer(getReceiverAndAmount());
In general I think tuples should be proper types. I used to dislike this syntax too, but nowadays I actually like it. It's succinct, clear, and already used by other languages.
array brackets makes me think elements are of the same type, which is not true.
I think it depends on what each of us is used to. To me the .0
syntax feels a bit like I'm abusing some internal mechanism that exposes element indexes as properties on an object. It just looks weird. On the other hand, having used Python a lot, using brackets to index tuples feels completely natural and intuitive.
In any case, I agree that it would be nice to have tuples as proper types or at least some way to access elements, whatever syntax we might eventually settle on.
Do we now have this?
Abstract
Tuples elements can be accessed with indexes. Let's have it!
Motivation
Other programming languages have this feature, it's quite useful. I believe code could be more expressive with this feature.
Specification
Tuple could be called anonymous struct, with both name and field names anonymous. We could use indexes to have access to individual fields:
Backwards Compatibility
Not sure there is any way to make this backward compatible.