solidity icon indicating copy to clipboard operation
solidity copied to clipboard

Tuple elements index access

Open k06a opened this issue 3 years ago • 9 comments

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:

function foo() public view returns(uint256 value, bytes memory data) { ... }

foo().0
foo().1

Backwards Compatibility

Not sure there is any way to make this backward compatible.

k06a avatar Feb 05 '22 14:02 k06a

The main problem I see is that tuples are not "proper" types in solidity.

chriseth avatar Feb 07 '22 12:02 chriseth

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?

cameel avatar Feb 07 '22 12:02 cameel

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.

chriseth avatar Feb 07 '22 13:02 chriseth

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.

ekpyron avatar Feb 07 '22 15:02 ekpyron

@cameel array brackets makes me think elements are of the same type, which is not true.

k06a avatar Feb 12 '22 20:02 k06a

Also I remember someone asked for ability to use tuples as arguments in function calls:

token.transfer(getReceiverAndAmount());

k06a avatar Feb 12 '22 20:02 k06a

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.

leonardoalt avatar Feb 14 '22 16:02 leonardoalt

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.

cameel avatar Feb 14 '22 21:02 cameel

Do we now have this?

obumnwabude avatar May 13 '24 06:05 obumnwabude