r/javascript • • Feb 21 '22

Future Javascript: Records and Tuples

https://dev.to/smpnjn/future-javascript-records-and-tuples-14fk
315 Upvotes

116 comments sorted by

75

u/RealMercuryRain Feb 21 '22

Nice. Finally no immutable.js in dependencies.

64

u/iamnearafan Feb 21 '22

And finally comparison by equality rather than identity.

38

u/editor_of_the_beast Feb 21 '22

It's hard to describe why this is so important for people that haven't experienced it. But I've been saying it for a long time - value semantics is going to change your life.

15

u/RealMercuryRain Feb 21 '22

True

21

u/takecarebye Feb 21 '22

You mean 1?

17

u/Akkuma Feb 21 '22

'No'

0

u/[deleted] Feb 21 '22

[deleted]

5

u/HasoPunchMan Feb 21 '22

'undefined'

4

u/unHolyKnightofBihar Feb 22 '22

You are all falsey

2

u/TakeFourSeconds Feb 22 '22

If you're starting with an object and then converting to compare, how will comparison work with deeply nested objects? Can you just convert at the root level and then compare? Otherwise, it's basically the same problem that exists now.

3

u/sharlos Feb 22 '22

All nested objects are made into immutable Records.

1

u/NoInkling Feb 21 '22

Because of the way they're implemented it's essentially both.

1

u/IDart123 Mar 02 '22

Anyone knows, can i spread usual object to record? I wasn't find this

26

u/Kablaow Feb 21 '22

As someone who is still learning JS (3 years exp lmao) what would this be used for?

Just to make "safer" code?

62

u/iamnearafan Feb 21 '22

The main use case is if you do { a: 1 } === { a: 1 } in JS, you'll get false - since they both have different "identities". If you use records, #{ a: 1 } === #{ a: 1 } will return true, since records and tuples are going to allow us to compare value, rather than identity.

This lets us easily compare complicated API responses (for example) to 'expected' results, rather than having to build often quite annoyingly complicated functions to do this comparison.

19

u/heyitsarpit Feb 21 '22

won't this be really slow for larger object ? I feel like an equality check with records and tuples will be O(n), where n is number of leaves in the structure. Where as checking equality with an id will always be O(1).

13

u/iamnearafan Feb 21 '22

I'm not sure how it's implemented under the hood to be honest. They could use IDs - it may not necessarily be O(n).

2

u/BowlingSashimi Feb 21 '22

Unless I'm missing something obvious, you can't do equality checks with IDs, can you?

7

u/hekkonaay Feb 21 '22

As long as the keys are sorted, you can just compare hashes, which is basically O(1)

2

u/meisteronimo Feb 22 '22

Hashing algorithms for comparison. This is the way.

1

u/WhatArghThose Feb 22 '22

You basically just concatenate the keys to a string, hash the string in SHA-256, and compare...is that right?

2

u/meisteronimo Feb 23 '22 edited Feb 23 '22

Sha-256 would be expensive. The feature they will likely implement is called "interning". It already exists for strings. When you create 50 strings in js that are the same, js points the 50 vars values to the same memory block. This is how string === comparison is constant speed. The JS engine doesn't need to compare the letters, the entire 50 vars values are pointing to the same memory block. As soon as you have immutable data types, it makes this pointing memory blocks easier and more predictable.

The exact way the JS engine will know at the tuples creation time that it already exists in memory I'm not sure, I assume it's some type of short hash table to find the memory block that has already been created by other tuples of the exact same value.

Python for instance does interning on strings only, but does allow === comparison of arrays and tuples but it's not optimized by interning

The modern js runtimes are killer efficient so I just assume they will have a similar optimization that I described already exists for js strings.

2

u/190n Feb 22 '22

Many interpreted languages use something called "string interning" which basically means that every unique string is only ever stored once in memory, in a giant hash table, and you can compare strings by just comparing pointers. In theory you could extend this to records and tuples, although I don't know if it would be a good idea.

19

u/lo0l0ol Feb 21 '22 edited Feb 21 '22

it's not to test if they are the same object. it's to test if the values are equal. they are going to have separate ids since they are separate objects records. here's the example given in the proposal:

const ship1 = #[1, 2];
// ship2 is an array:
const ship2 = [-1, 3];

function move(start, deltaX, deltaY) {
  // we always return a tuple after moving
  return #[
    start[0] + deltaX,
    start[1] + deltaY,
  ];
}

const ship1Moved = move(ship1, 1, 0);
const ship2Moved = move(ship2, 3, -1);
console.log(ship1Moved === ship2Moved); // true
// ship1 and ship2 have the same coordinates after moving

9

u/natziel Feb 21 '22

Usually a data structure called a hash array mapped tries (or HAMT) is used, and yeah checking for deep equality is necessarily slower than checking for shallow equality. There are a lot of optimizations that can happen under the hood with HAMTs though, whereas there isn't much that you can do to optimize deep equality comparisons with plain objects

6

u/NoInkling Feb 21 '22 edited Feb 21 '22

Look into how Immutable.js is implemented. If you can find it, I remember there was a talk from an old React conf where it was explained how they can be efficient. Basically, structural sharing is used. If two records are the same, they essentially point to the same thing under-the-hood.

Edit: I believe this was the talk: https://youtu.be/I7IdS-PbEgI?t=303

2

u/[deleted] Feb 21 '22

Not sure how they will be implemented, but since they’re immutable it might make sense to tightly pack all the contents as one allocation. So equality comparison would basically be a single memcmp(), like comparing strings.

1

u/HetRadicaleBoven Feb 21 '22

Well, API responses might be a bit more difficult, I think, since I don't think this'll be added to JSON?

Edit: nvm, looks like they thought of that! https://www.reddit.com/r/javascript/comments/sxq55p/comment/hxu5aey/?utm_source=reddit&utm_medium=web2x&context=3

19

u/Cptkrush Feb 21 '22

The biggest thing that I can tell is the value based deep comparison for objects and arrays. So right now if you make const obj = {} and const obj2 = {} then run obj === obj2 you will get false. The way I understand it is JavaScript is only checking to see if those two objects are the exact same reference (identity) rather than checking if both of those objects have equal values. With a record and tuple, you could convert your objects to a record, and convert your arrays to tuples to make value comparisons. So when you compare #{…obj} === #{…obj2} you would get true. It’s actually super useful!

6

u/Kablaow Feb 21 '22

Ah that's cool!

2

u/senocular Feb 21 '22

The biggest thing that I can tell is the value based deep comparison for objects and arrays.

To be clear, it doesn't let you compare objects and arrays, rather records and tuples. And they're restricted to only contain other primitive values. So you'll be limited in what comparisons you can make and something like #{…obj} === #{…obj2} may not work if either obj or obj2 have any values that aren't compatible with records (e.g. nested objects, functions, etc.).

4

u/Cptkrush Feb 21 '22

Yeah I mean I wasn’t attempting to provide real world code that should be used, I was just trying to shorthand how I interpreted the idea. But yeah, who knows if spreads will work like that. As far as comparing primatives, that’s a fair point, and if you plan on deep comparing nested objects, you’ll probably want to write some kind of helper that automatically converts your entire object including child objects into records.

8

u/senocular Feb 21 '22

The upside of this is most serialized data is already this - some structured collection of primitives in objects and/or arrays. And they plan on updating JSON with a parseImmutable() which will let you go straight from JSON into Records/Tuples.

2

u/Cptkrush Feb 21 '22

Oh sick, that will be super useful

5

u/Silencesound Feb 21 '22 edited Feb 21 '22

One of the problems with the current mechanism of reference passing is about shallow copy vs deep copy in nested objects.

Example: if u copy an array without any nested object inside using a spread operator on an another array, modifying this copy will not mutate the original array; BUT in case u attempt to shallow copy (spread operator produces a shallow copy) an object with a nested object inside, you have to mind that the nested object will be passed by reference so, if you will modify it in the copy, since the nested object in your copy it's just a reference, the original nested object (so the original array) will be modified too.
This can lead to nasty messes especially if u are not aware of this mechanism.

Plus this will be of great importance if you're trying to write pure functions!

4

u/ic6man Feb 21 '22

Yes. And the code that uses immutable values will be more performant.

This can often be seen as counter-intuitive but modern garbage collectors are built to create and destroy short lived immutable objects and excel at it.

Longer lived mutable objects are actually more difficult for the garbage collector and yield worse performance (that is also less predictable).

The side benefit is that it gives you deep equality but contrary to the some other answers to your question I don’t believe this is the main motivation.

Also note that immutable values are much more in line with functional programming with aligns better with frameworks like React so it’s really a holistic approach of moving more toward safe data handling practices.

1

u/editor_of_the_beast Feb 21 '22

That's definitely one of the benefits. Currently everything in JS is reference-based. When you pass an object into a function for example, the reference is passed, not the data itself. When you compare two objects, their reference is compared, not their values. The implication of this is that you have to worry more about specific objects instead of their values, which can cause unnecessary coupling in my experience.

When you move to operating on values, the specific objects don't matter anymore, and you immediately stop depending on mutation to do things, which is a good thing for simplicity.

7

u/Silencesound Feb 21 '22 edited Feb 21 '22

Currently everything in JS is reference-based

This is not correct: JS passes all primitives (strings, numbers, bigInt, boolean, symbols, null and undefined types) by value and all object types by reference.

3

u/editor_of_the_beast Feb 21 '22

That’s correct. One more addition - JS is call by value only, but for objects it is the value of the reference that’s passed. It’s not technically pass by reference.

2

u/Silencesound Feb 21 '22

Thanks, I dug that a little bit further and I found this Dmitry Soshnikov's article about evaluation strategies that's pretty illuminating.

So, the right definition for JS behavior with objects is a "call by sharing". Pretty nice to look at the comparison made through C++ pointers of the "by reference" and "by sharing" mechanisms.

1

u/editor_of_the_beast Feb 21 '22

Yea it's pretty tricky stuff, I always have to go back and think about which one is which. I just know that pass by reference is very rare, almost no languages actually implement it.

30

u/LloydAtkinson Feb 21 '22

Really not looking forward to the problems this will cause with typescript tuples…

19

u/sonaxaton Feb 21 '22

Not to mention TS already has a ubiquitous type named Record

22

u/LloydAtkinson Feb 21 '22

That also doesn't even do the same thing!

9

u/NoInkling Feb 21 '22

It's really just a single utility type that's shorthand for an object type with an index signature though, there's no real reason (besides breaking backwards compatibility) that it can't be renamed to something else.

I foresee users saying "ES record" (and "ES tuple") to differentiate for a while though.

42

u/shgysk8zer0 Feb 21 '22

Nah. Typescript created the problem by polluting the global namespace.

12

u/BarelyAirborne Feb 21 '22

I'm getting Internet Exploder flashbacks.

9

u/zarmin Feb 21 '22

Exploder

It stays

11

u/MrJohz Feb 21 '22

How did Typescript pollute the global namespace here? While TS has a concept of tuples distinct from this proposal, which could cause some verbal confusion, there is no confusion in syntax — TS tuples are simply a way of typing fixed-length arrays, while the proposed tuple syntax is a separate datatype with a separate syntax.

16

u/shgysk8zer0 Feb 21 '22

That's irrelevant to polluting the global namespace. Typescript uses e.g. Record rather than something like TypeScript.Record, so is polluting the global namespace.

Polluting of the global namespace and prototype pollution (adding to a prototype other than for polyfilling) have been known for a long time now to be terrible ideas because those practices cause problems if/when they are to be officially added to JavaScript. Often times, if a library is popular enough, they cause a spec to have to be rewritten or renamed so that the addition doesn't break the web. Sometimes they cause proposals to be abandoned, thus holding back JavaScript as a whole.

It's a terrible idea to define new types/classes on the global namespace, and it's a terrible idea to invent new methods for built-in classes. Don't do it!

11

u/MrJohz Feb 21 '22

Ah, yeah, I guess there's some possibility for confusion there.

FWIW, this is unlikely to be a major issue for most people: the TS type of a record is probably going to be record (lowercase, just like the type of a primitive string is string and not String), and the compiler can distinguish between when a name is used as a type, and when it is used as a value, so there won't be any issues with using the constructor methods on the Record namespace. I imagine the biggest issue is just going to be confusion.

15

u/ragnese Feb 21 '22

I'd be willing to bet that TypeScript deprecates the Record type in favor of a differently named replacement.

TypeScript tuples aren't even named anywhere in the language, are they? Maybe they can just start calling them something else...

1

u/mikejoro Feb 22 '22

Why would typescript call them something else though? That's what a tuple type is. Js is just implementing a tuple type which is enforced at runtime (effectively). TS will know any runtime tuples are of course also a tuple type for compile time checking.

2

u/ragnese Feb 22 '22

Simply because it's confusing. I agree that "tuple" is the appropriate name for the concept, but not when there's going to be something else also called "tuple".

I can potentially see a way to merge the concepts, though: maybe when you write readonly [number, number], the TypeScript compiler can actually spit out an immutable JavaScript-native tuple. Then when you write just [number, number] TypeScript can refer to that as a "mutable tuple" or just a "sized array" or something.

2

u/mikejoro Feb 22 '22

I get what you are saying about it being confusing, but because typescript doesn't actually use a keyword called "tuple", I don't think it's an issue. It's more confusing (from my perspective) to try to create different terminology than the correct one. You want language featuea to be googleable to someone who knows what the terminology is without knowing language specific implementation details. It's not a coincidence js has decided to name these tuples, after all.

Imo typescript will just handle this the same as they did with private. If you use the "readonly tuple" native js syntax, ts will properly type it that way; if you use the ts one, it's only a compile time enforced check.

6

u/[deleted] Feb 22 '22 edited Feb 22 '22

[removed] — view removed comment

3

u/shgysk8zer0 Feb 22 '22

Where do you get the idea this is about defining something named Record? This is specifically about conflicts when declaring types.

const record: Record<...> = {...}
// vs
const record: Record = #{...}

... As well as types for arguments and return values.

1

u/[deleted] Feb 22 '22 edited Feb 22 '22

[removed] — view removed comment

3

u/shgysk8zer0 Feb 22 '22

The first Record is this: https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type

The second is the new JavaScript one, where #{} instanceof Record is presumedly true. Maybe Typescript would lowercase it like String, but I'd hate to see record and Record be entirely different things.

My proposed solution, and what I say Typescript should've done to begin with, is to have put any and all additions into a namespace. So there would be maybe TypeScript.Record or TS.Record to avoid any confusion or conflict with anything JavaScript has and might add. Of course, Typescript should also provide a mechanism of aliasing that, so the developer could explicitly declare that they were using Record from the TypeScript namespace without having to type it out everytime. Maybe follow other languages and use

use Typescript.{ Record, Foo };

Or

const { Record, Foo } = Typescript;

Or something like that. That would be how you design a language like Typescript to avoid potential future conflicts with JavaScript.

2

u/[deleted] Feb 22 '22

[removed] — view removed comment

2

u/shgysk8zer0 Feb 22 '22

Yeah, I just used that instanceof bit to be absolutely clear that Record there referred to a #{}. But I do think that I understand the distinction you were making between types and values now. It's a peculiar concept to convey, partly due to the weird space TS exists in... But yes, I was ultimately thinking of types as being the TS equivalent of instanceof, but with types being a purely TS thing. And I was thinking of "value" in the sense of what is assigned to a variable/constant. const name: type = value. And my thinking was that, if record instanceof Record was valid in TS, then const record: Record = #{} would be as well.

I know TypeScript isn't likely to change things and that such changes would break things if implemented. I'm just saying it would've been better if TS were designed that way from the beginning. Record is only an example of the potential conflicts, and basically anything that TS has added could cause conflicts at some point, or even prevent something from being added to JavaScript.

Having said that though, I do know that Angular made a similar drastic change after it was widely used. PHP has also introduced breaking changes with its major versions. So it wouldn't be without precedent for TS to have a major version where a TypeScript namespace was introduced to import from, everything was still globally available, there was a flag or something to toggle warnings/errors for using the global ones, defaulting that flag to throw late in the lifecycle of that major release, and removing them from global on the next major release. It might cause complaints and headache, but I think something like that needs to happen.

Not sure about things like interface though, as keywords don't make any sense on a namespace. But interface existing in TS does effectively prevent JS from ever getting interfaces.

20

u/[deleted] Feb 21 '22 edited Jul 02 '23

[removed] — view removed comment

4

u/sharlos Feb 22 '22

You're making it sound like a bigger deal than it is, renaming all uses of Record to Dictionary or something else is pretty easy.

2

u/[deleted] Feb 23 '22

[removed] — view removed comment

2

u/sharlos Feb 23 '22

Okay, but it is just JavaScript with types. If you take some TS code and remove the types, all you have left is JavaScript. That's the whole point.

If you're using experimental features like decarators you don't really have much room to complain about breaking changes.

Your arguments about catching up to a changing JS standard applies just as much to browser implementations of JavaScript as it does to TS and it's not really an issue at all. Yes TS is exposed to potentially backwards compatibility breaking changes now and then, but that's hardly unique to TS, plenty of languages have to deal with similar issues.

Typescript could deal with minor issues like this every second month and the advantages of TS would still be worth it for me and plenty of other devs, it's fine if you don't see the appeal, no one is forcing you to do anything.

7

u/musical_bear Feb 21 '22

Why would this create issues? TypeScript doesn’t contain special syntax, or even a special type name for tuples. A TypeScript tuple is just a normal array where the number of members and type of all members can be known at compile time. In other words, it’s just extra tooling to help give you type safety even for all items within some special case arrays. As a common example, the return value from React’s “useState” function is technically a “tuple” in TS, but in reality it’s just an array where we know it has two members and the types of those two members.

TypeScript tuples don’t change how arrays behave at runtime, as this JS syntax and proposal does. At worst, all this does is add some confusion as to which kind of “tuple” a developer should use, but luckily to decide between the two you’d ask yourself the exact same questions you’d already ask yourself anyway in JS when choosing between a normal array and a tuple.

-3

u/LloydAtkinson Feb 21 '22

I trust you didn't see the disaster that was decorators

12

u/musical_bear Feb 21 '22

What do decorators have to do with tuples…?

-2

u/senocular Feb 21 '22

They're both features TypeScript had first.

7

u/HetRadicaleBoven Feb 21 '22

Decorators is different, because it's a language-level construct that was added under (friendly) pressure of the Angular team, who were considering forking TS to add them.

That said, that's also the reason why they were always under an experimental flag. If you didn't enable that, native JS decorators won't be a problem for you. And neither should this be.

1

u/sabababoi Feb 21 '22

So what happens when this feature is released and I use type "Record" in my ts code? Do I get JS record or TS record?

7

u/PickledPokute Feb 21 '22

Record as a type with TS will stay the same.

Typing a record will be done with (param: #{ n: number, s: string })

1

u/sabababoi Feb 21 '22

Seems very unergonomic.. I would have expected it to look like Record<number, string, bool>

7

u/MrJohz Feb 21 '22

It's basically the same way that object type are defined: (param: { n: number, s: string })

Of course practically, you'd probably move both into a type alias:

type Param = #{
    n: number;
    s: string;
}

// Or the existing object syntax: type Param = { n: number; s: string; }

1

u/sabababoi Feb 21 '22

Yeah I suppose that right and won't really be a problem

1

u/sharlos Feb 22 '22

You can't type the property names with syntax like that.

1

u/sharlos Feb 22 '22

Might also use readonly.

1

u/PickledPokute Feb 22 '22

readonly is not the same since it doesn't prevent from adding new properties, etc.

4

u/[deleted] Feb 21 '22

So then will Typescript's Record utility type need to be updated?

4

u/PrinnyThePenguin Feb 21 '22 edited Feb 22 '22

Does anyone know how is this feature going to interract with React's comparison of props? Is it going to eliminate the need for memo?

2

u/ninja_lazorz Feb 22 '22

At least you dont need to memo as much

2

u/sharlos Feb 22 '22

It will make memo even more useful, you can pass a nested record as a dependency and recalculate values that use the record without needing more complex comparisons.

1

u/fzammetti Feb 22 '22

Does anyone know what this shows:

console.log( [1, 2, 3] === #[1, 2, 3] );

?

I ask because if that doesn't return true then I'm not sure I honestly see the use of Records and Tuples. It makes a ton of sense to me to have a Record or Tuple that I compare an object or array to later to see if it's truly identical, if the object or array has diverged from a "baseline", so to speak, but if all I can actually do is compare Records to Records and Tuples to Tuples... which is the only example the article shows... then it doesn't seem especially useful to me.

Well, I partially take that back: it makes sense in the context of functional programming, or a completely immutable style of coding. But we can't pretend that's the only flavor of JS being written today, or even the majority at this point, so being able to compare these new types to POJO's strikes me as the more generally useful use case, hence my question.

2

u/iamnearafan Feb 22 '22

That will return false but you can inter-convert between Records and Objects - so you can always turn an Object into a Record for value comparison. I agree tho, think it would've been nice to compare an Object to a Record.

3

u/AlGoreBestGore Feb 21 '22

Not a fan of the syntax. Why not just new Tuple()? Seems like it would be easier to polyfill too.

2

u/NoInkling Feb 21 '22

1

u/voidvector Feb 22 '22 edited Feb 22 '22

I don't think that's sufficient justification for a syntax-level feature. User can trivially alias a stdlib API to accomplish the same goal:

const $I = Object.createImmutableRecursive;
const record = $I({
    a: {
        foo: "string",
    },
    b: {
        bar: 123,
    },
    c: {
        baz: {
        hello: [
            1,
            2,
            3,
        ],
      },
    },
});

IMO, there are two features here, and they should be decoupled:

  • Immutable data structure w/fast comparison support from the engines

  • Special syntax for immutable data structure

JSON is an industrial standard outside of JavaScript. New JS features should try to augment it, rather than side-step it.

1

u/[deleted] Feb 22 '22

[removed] — view removed comment

1

u/Impressive-Flan-1656 Feb 22 '22

Who downvoted? It is 404?

1

u/NoInkling Feb 22 '22 edited Feb 22 '22

I think whatever app you're using might be breaking the link for you (maybe to do with the auto-linking). On the web version (both old and new) it works. Maybe try copy-pasting the text instead. Or see if this one works.

2

u/droctagonapus Feb 21 '22

Can you destructure these?

3

u/[deleted] Feb 21 '22

Yes

1

u/Practical_Drag_9267 Feb 22 '22

Decorators is different, because it's a language-level construct that was added under (friendly) pressure of the Angular team, who were considering forking TS to add them.

That said, that's also the reason why they were always under an experimental flag. If you didn't enable that, native JS decorators won't be a problem for you. And neither should this be.

Great question. Would love to know the answer as well.

2

u/bentinata Feb 22 '22

Why do conversion from Array into Tuple need spread?

1

u/senocular Feb 22 '22 edited Feb 22 '22

You don't need spread. You can use the static from() methods instead. The constructor functions also work, though Record takes an object whereas Tuple takes a list of values (rather than an array).

Record({prop: value}) // works
Tuple(1,2,3) // works
Tuple([1,2,3]) // no worky

2

u/bentinata Feb 22 '22

Why is it different from Record? There's no Record.from.

2

u/senocular Feb 22 '22

Records use fromEntries() since they're key-value pairs, just like how you have Array.from() and Object.fromEntries() but not the other way around.

1

u/foxleigh81 Feb 21 '22

Finally! I really miss this in JS after coding in Python for a while.

1

u/UnexpectedLizard Feb 22 '22

So... I'm gonna go against the grain here.

I don't see what this provides that arrays and destructuring didn't before.

Yes, it's immutable, but that's just about never needed in the real world in my experience.

0

u/[deleted] Feb 22 '22

[deleted]

1

u/[deleted] Feb 22 '22

Anyone who values immutability wouldn’t have been using pure JavaScript in the first place, they’d be using Typescript on web and anything else for other use cases.

5

u/double_en10dre Feb 22 '22

Well no, that’s not true. Many people who value immutability are forced to use pure JavaScript because they inherit projects that do. And you can’t just convert a gigantic codebase to ts on a whim

You can convert it incrementally, yeah. But it’s incremental. Lots of the code will be untyped. So immutability helps.

Plus you’re kidding yourself if you think typescript == no need for immutability. Typescript helps, but it doesn’t fix everything. In the real world, there’s still a lot of “any” declarations out there in both our own code and the packages we use

Making the actual language enforce immutability will be an absolute game changer

-1

u/[deleted] Feb 22 '22

Typescript is the place for immutability. Don’t get me wrong, I am a fan of immutability. I’m just saying that those who value immutability typically would have drawn the line at one of the many more severe issues with JS and utilized Typescript or a different language.

There are very few reasons anyone should be using JavaScript over Typescript at the moment and in all of those edge cases, immutability isn’t something that will add to the code base in my opinion. It’s already using JavaScript, which is a mess to begin with. Adding #{} and #[] is gross, but gross is par for the course for JavaScript.

I don’t think this needs to be a built in feature.

0

u/[deleted] Feb 24 '22

[deleted]

0

u/[deleted] Feb 24 '22

I think the syntax for this is gross

1

u/[deleted] Feb 21 '22

So awesome!!

0

u/shitepostx Feb 22 '22

Can't wait until some dumbass manager forces their coworkers to excessively use Records / Tuples, and they have some gigantic GC nightmare on their hands.

0

u/[deleted] Feb 24 '22

[deleted]

0

u/shitepostx Feb 25 '22

sorry you write input forms for a living

0

u/[deleted] Feb 25 '22

[deleted]

0

u/shitepostx Feb 25 '22

Alright, I'll cut the crap.

I've worked on a lot of R&D projects involving graph-based structures (doubly linked lists.)

Imagining reconstructing a graph for a large open-api document (one example) every time you update a single node's data-entry.

It's inconceivable.

-1

u/tbranyen Feb 21 '22

Wonder what the underlying type via typeof operator is. We currently have 7 unique types with the introduction of Symbol, does this update bring us to 9? Or will thse work like Set and Map and return object?

If its the former, getting old man vibes about bloating the syntax. Could we not have solved these problems in a more JavaScripty manner without altering syntax? Object.compareValues(objA, objB) for instance.

I get that its possible to bend the rules for object-like values to compare by value, and its nice for folks who come from other languages. I just worry many of these features aren't solving huge issues, like the new private syntax, causing work for implementers for how much adoption?

10

u/senocular Feb 21 '22

These (Record and Tuple) are both primitives so they'll have their own typeof values as well. For Record it will be "record" and Tuple, "tuple".

https://tc39.es/proposal-record-tuple/#table-35

Expect more to come too. Another proposal, Big Decimal, will be another (though only in stage 1 right now).

Also, comparison isn't the only advantage of these kinds of values. Being immutable is also a big part of it as they ensure that deep values cannot be unexpectedly changed.

0

u/[deleted] Feb 21 '22

[deleted]

8

u/editor_of_the_beast Feb 21 '22

And this is exactly why we need language features like this added to JS.

1

u/[deleted] Feb 21 '22

That only works if they’re in the same order