Tuple projection

I think we should get this feature. It’s too verbose to use Pair.second and there’s no ergonomic way to get one field of tuple with components more than 2.

Could you do a TLDR; of how you imagine this looking in ReScript?

Just like it in MoonBit, use a.0 to access first component, a.1 to access second component, …

So a.0 would be a->fst, a.1 is a->snd , etc?

I think so.

Try https://try.moonbitlang.com/, select 007_basic_tuple.mbt and check
“Accessing tuple elements”.

Add a line println(tuple.3) and see the magic:

i miss this sometimes (fst and snd are deprecated i think). but i usually do tuple destructuring, e.g. let (_, x) = tup. but yes, using .1 would be good (as for named tuple fields, i don’t really care for that, to me it looks like namedtuples in python or something, we can just do a type or object for that)

you can indeed define records that compile to arrays in rescript, that’s a nice way to replace tuples with something more ergonomic:

type t = {
  @as("0") foo: int,
  @as("1") bar: string
}

let t = { foo: 13, bar: "bar"}
let bar = t.bar