ethereum / solidity

Solidity, the Smart Contract Programming Language
https://soliditylang.org
GNU General Public License v3.0
23.35k stars 5.77k forks source link

Tuple elements index access #12635

Open k06a opened 2 years ago

k06a commented 2 years ago

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.

chriseth commented 2 years ago

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

cameel commented 2 years ago

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?

chriseth commented 2 years ago

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.

ekpyron commented 2 years ago

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.

k06a commented 2 years ago

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

k06a commented 2 years ago

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

token.transfer(getReceiverAndAmount());
leonardoalt commented 2 years ago

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.

cameel commented 2 years ago

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.

obumnwabude commented 6 months ago

Do we now have this?