Array typed with fixed length?

Hi !
So, my goal is to check at build time if two arrays have the same length.
In typescript, you can specify a fixed size for an array like:
let t = Array<2, string>
Is it possible in rescript ? If not, what is the recommand way to check if two arrays have the same lenth at build time ?
Thank’s :slight_smile:

I guess use a tuple for this!

2 Likes

Wow. Nice catch, I feel dumb now haha thank’s :slight_smile:
Here is an example for what is worth: https://rescript-lang.org/try?code=C4TwDgpgBAzsCuAzRAeA5DAfFAvFAFBgDRTGkwCUAUFQDYTBQCGUAXLAsigJYB2w2PPgCMJAEwkAzNXqMARmw5JUcAE58A5oIIAiRAHt9OkjpD74AC3PGoOuU1U7qVAPQuoASV4A3JrW4AJq7uslAAxopwyihqmtr4eoY2puZW8E5UQA

1 Like

Hum… in the case that I can have severals tuples with differents size (e.g: most of the time 2 values, but can be 5 or 10).
Anyway to simplify that and not having to write let TenTimeStuff: (string, string, string…) ?

Maybe give more context on what you’re trying to achieve

1 Like

Sure. I want two tuples with the same size (but different type in each, same type for every value in a tuple, like my previous example).

But the size can change according to the evolution of my code, it would be 2 or 3 most of the time, but it could be 10 or 20.

I want to avoid to write each size of tuple, and having variable name like TenTimeStuff. Or at least in a more compact way (something similar to typescript way could be cool, but I doubt we can do that).

So far I manage to have something like this: https://rescript-lang.org/try?code=C4TwDgpgBAzsCuAzRAeA5DAfFAvFAFBgDRTGkwCUAUFQDYTBQCGUAXLAsigJYB2w2PPgCMJAEwkAzNXqMARmw5JUcAE58A5oIIAiRAHt9OkjpD74AC3PGoOuU1U7qVAPQuoASV4A3JrW4AJq7uslAAxopwyihqmtr4eoY2puZW8E5UQA.

I would like to avoid extra data for the variant though.

I dunno if it’s enough context ?

Not sure if I got with tuple though, as I can’t access inner value with index as I can with array… :confused:

I guess tsnobip wanted to know concrete details on what data you are handling, and what APIs you are designing (and how these apis are supposed to be used)…what benefit does the size restriction of an array give to you? If you want to restrict an api to only pass in array<string> of size 2, why not create an api that handles an abstract type and provide some validation steps?

You can type a “Length List” with GADTs, but there is a parser bug which prevents you using ReScript syntax for it.

See here how that is implemented in ReasonML syntax (need to scroll down a bit).

It seems to be overkill to me, but it’s possible when you define the types in a .re file.

1 Like

Note that this GitHub issue is only about redefining list syntax. You can still implement diff lists with normal variant syntax.

But you probably want to just use tuples instead of that. The GADT diff lists are more interesting in theory than they are useful in practice.

1 Like