hsutter / cppfront

A personal experimental C++ Syntax 2 -> Syntax 1 compiler
Other
5.24k stars 224 forks source link

[SUGGESTION] Support "dot index" access for tuples as alternative for `std::get` #1027

Open bluetarpmedia opened 3 months ago

bluetarpmedia commented 3 months ago

Cpp2 could support tuple indexing expressions like Rust and Swift as syntactic sugar for std::get.

main: () -> int = {
    bag: std::tuple = (1, "test", 3.14);
    std::cout << bag.1 << "\n";         // Instead of std::get<1>(bag)
    return bag.0;                       // Instead of std::get<0>(bag)
}

Will your feature suggestion eliminate X% of security vulnerabilities of a given kind in current C++ code? No

Will your feature suggestion automate or eliminate X% of current C++ guidance literature? No

JohelEGP commented 3 months ago

Seems like something you could implement with Cpp1 reflection (I think it's in the paper).

fro0m commented 3 months ago

Looks like operator[] might be an option for accessing tuple members because in terms of usage, tuple is a collection that can be accessed with indexes. Also for example in Python the interface of tuple is similar to list with accessing with [index].

jcanizales commented 3 months ago

The index needs to be known at compile time though, as the return type depends on it.

bluetarpmedia commented 3 months ago

The index needs to be known at compile time though, as the return type depends on it.

Yes, the tup.1 syntax proposed above would be lowered to std::get<1>(tup).

jcanizales commented 3 months ago

I know I was replying to the suggestion to use operator[]. Although maybe such a member function can indeed be written with Cpp2's existing tools?