DEV Community

Cover image for Future Javascript: Records and Tuples

Future Javascript: Records and Tuples

Johnny Simpson on February 21, 2022

Records and Tuples are an upcoming feature for Javascript, which may be familiar if you have used other languages. They are very similar to Arrays ...
Collapse
 
aandraz profile image
Andraž Kos
[1,2] === [1,2]
false

{1:2} === {1:2}
false

JSON.stringify([1,2]) === JSON.stringify([1,2])
true

JSON.stringify({1:2}) === JSON.stringify({1:2})
true
Enter fullscreen mode Exit fullscreen mode

[drops 🎤]

Collapse
 
smpnjn profile image
Johnny Simpson

You solved Javascript

Collapse
 
opensas profile image
opensas • Edited

This is great news, but I wonder if there's a reason why we can't have something like this instead of the "#" thing

val myRecord = {
    name: "New Record",
    tags: ['some', 'tags', 'go', 'here']
}

val myTuple = ['some', 'other', 'set', 'of', 'array', 'items'];
Enter fullscreen mode Exit fullscreen mode
Collapse
 
smpnjn profile image
Johnny Simpson

You can use Record() and Tuple() afaik if you have that preference

Collapse
 
opensas profile image
opensas

Yes, I understood it, I just think it would be more elegant and clear.
Here's my message on the thread discussing this proposal:
twitter.com/opensas/status/1499729...

Collapse
 
petsel profile image
Peter Seliger • Edited

The most obvious reason is to be able to pass records and tuples directly to functions as literals. Thus, in order to let the engine know how to handle such data, it needs its own syntax. Just another variable keyword doesn't cover this in future very common use case.

Collapse
 
mdmazzola profile image
Matt Mazzola • Edited

Good post. Given the mutation operations on records and tuples are common way to simulate immutable behavior now hopefully the transition will be as simple adding # sign in certain cases.

It would be interesting to see the performance differences between the generation of new object using spread vs generating new record using spread and likewise with tuples.

Based on your first example of a record nested in record, it seems that there would be benefit to having a "deep record" symbol to implicitly make nested arrays or objects also become records or tuples.

Collapse
 
rajeshdixitsatyanarayan profile image
Rajesh Dixit

This looks great but I wonder the reasoning behind this. We can still achieve this using Object.freeze, so what is the benefit? Only thing I can see is compare by value but that does not seem that big of a reason to create new datatypes in a language. One thing I can think would help though is using Set/ Map with these. You can store complex record and to fetch it, all you need is set.get(#{...}).

Collapse
 
amymc profile image
amy mccarthy • Edited

Object.freeze isn't deeply immutable. From the MDN docs:

"The result of calling Object.freeze(object) only applies to the immediate properties of object itself and will prevent future property addition, removal or value re-assignment operations only on object. If the value of those properties are objects themselves, those objects are not frozen and may be the target of property addition, removal or value re-assignment operations."

Collapse
 
pez profile image
Peter Strömberg

The talk ”Solving Problems the Clojure Way”, by Rafal Dittwald is also relevant here since he is using JavaScript like if the objects and arrays were immutable in the example: youtube.com/watch?v=vK1DazRK_a0

Collapse
 
pez profile image
Peter Strömberg

This is great news! Anyone who wants to understand just how great should watch Rich Hickey's talk ”The Value of Values”: youtube.com/watch?v=-I-VpPMzG7c

Collapse
 
leo profile image
Leonardo Galante

Great post @smpnjn can you do a spread of a normal object in a record? same for arrays?