Skip to content

[RFC] Array spread syntax #6546

@zth

Description

@zth

Array spreads are quite common in JS land, and they're pretty awkward to do right now in ReScript. You essentially need to do this (using Core):

let arr1 = [1, 2]
let arr2 = [3, 4]
let arr3 = [5, 6]

let arr = Array.concatMany([7, 8], [arr1, arr2, arr3])
let arrOnlySpreads = Array.concatMany([], [arr1, arr2, arr3])

Which compiles to:

var arr1 = [
  1,
  2
];

var arr2 = [
  3,
  4
];

var arr3 = [
  5,
  6
];

var arr = [
    7,
    8
  ].concat(arr1, arr2, arr3);

var arrOnlySpreads = [].concat(arr1, arr2, arr3);

Compiling to concat is fine imo, but it'd be great if the ReScript above could be written like this instead:

let arr1 = [1, 2]
let arr2 = [3, 4]
let arr3 = [5, 6]

let arr = [7, 8, ...arr1, ...arr2, ...arr3]
let arrOnlySpreads = [...arr1, ...arr2, ...arr3]

This could parse to the first ReScript snippet using concatMany, that's fine.

I believe this would be a pretty harmless change (as of now you need to write the exact code it parses to manually) and being a significant gain in ergonomics. Remains to be seen how difficult it is technically (need to account for things like [1, ...arr1, 2, 3,4 ...arr2] etc).

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions