Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 2104

Tuple access by index (range)

$
0
0

You can do that in userspace today, but with less nice syntax and one function per tuple length.

module Tuple = {
  @get_index external get2: (('a, 'b), int) => 'a = ""
  @get_index external get3: (('a, 'b, 'c), int) => 'a = ""
  @get_index external get4: (('a, 'b, 'c, 'd), int) => 'a = ""
  @get_index external get5: (('a, 'b, 'c, 'd, 'e), int) => 'a = ""
  @get_index external get6: (('a, 'b, 'c, 'd, 'e, 'f), int) => 'a = ""
}

let myTuple = (1, "hello")

myTuple->Tuple.get2(1)->Console.log

let myTuple3 = (1, "hello", true)

myTuple3->Tuple.get3(2)->Console.log

[Playground Link]

So it would basically be just syntactic sugar, tuple length should be known at compile time as well so it seems doable.

Also, as we are on our way to get rid of the OCaml stdlib(s), we would like to get rid of fst and snd as they only support the first and second position and are of no use for larger tuples.

But at some point you should rather use a record anyways. Except maybe for special cases like React Hooks dependencies.


Viewing all articles
Browse latest Browse all 2104

Trending Articles