-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add JSON roundtripping goal and consequences #42
Conversation
That also precludes including frozen functions - then the proposal is not really about immutability, it’s about json round tripping, which means that mutable objects should be allowed if they can json round trip. |
What does Object.from do with things that aren’t records or tuples? |
|
@@ -22,19 +22,20 @@ ECMAScript proposal for the Record and Tuple const value types (also known as im | |||
|
|||
# Overview | |||
|
|||
The goal of this proposal is to introduce deeply constant/immutable value types to JavaScript. It has multiple objectives: | |||
The goal of this proposal is to introduce deeply constant/immutable data structures to JavaScript. It has multiple objectives: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That also precludes including frozen functions - then the proposal is not really about immutability, it’s about json round tripping, which means that mutable objects should be allowed if they can json round trip.
-
What alternative are you suggesting?
-
Is the issue that you see immutability and simple (de)serialization as non-orthogonal goals? If so, note that the proposal is already not 100% orthogonal:
- We could add immutable objects where
===
is pointer equality. In fact, JS libraries offer those already! Examples include ImmutableJS, Mori, and MicroState. - We could add mutable objects where
===
is structural. In fact, we could do that very easily by having a new well-known symbol,Symbol@@equals
, thereby allowing userland implementations of arbitrary equality-checking algorithms.
There are tradeoffs here. My hope is to bias toward providing good performance and developer ergonomics at the expense of some orthogonality.
- Would you agree that it will be beneficial to have predictable and useful serialization?
I believe the proposal was designed with ease of serialization/deserialization in mind, such as communicating data with workers. Keeping serialisation simple for now helps us build toward that goal.
You specifically mentioned functions:
- You mentioned frozen functions specifically. There is no special-casing for freezing in the current proposal. #{ a: Object.freeze({}) }
errors if I understand correctly.
- Functions are particularly nasty if we want a good (de)serialisation story. They aren't supported by JSON.stringify by default, and aren't supported at all by the HTML5 structured clone algorithm.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I'm a bit confused. If this proposal is about immutability, then any immutable object - including deeply frozen ones - seem like they should be included.
If it's primarily about JSON-serialization and structural equality, then there's not really any point in making them immutable - they could just be mutable JSON-serializable primitives.
It's worth looking into https://github.com/sebmarkbage/ecmascript-shallow-equal and why it was withdrawn - it seems like it might run into the same concerns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I said, in my interpretation the proposal has multiple, related goals. And there may be a tradeoff between orthogonality and usefulness.
Thanks for the link to the shallow equality proposal. That proposal seems to have been withdrawn for unrelated reasons: https://github.com/sebmarkbage/ecmascript-shallow-equal#status-of-this-proposal. In the shallow-equal proposal, the semantics weren't fully specced (for perf reasons), but vendors were afraid to implement because they sensed they would be locked in to certain implementations for backwards-compatibility.
related: #45 |
- Add Object.from(record) - Add behavior of JSON.stringify(tuple) and JSON.stringify(record) - Clarify what values are allowed as keys/values of records and as entries in tuples
I like the Record.from API, and I think the JSON serialization behavior is good documentation (it falls out of other things in this readme), but I don't understand the motivation for the JSON round-tripping property. It seems really odd to me to exclude symbols, undefined and BigInt. Further, I was hoping that this proposal would give us the core data model that would expand to "value classes" including private fields, which would definitely not JSON round-trip. And finally, sorting the keys breaks JSON round-tripping. Why not have as a goal, a subset of this will JSON round-trip? |
Thanks for your feedback, @littledan . I guess I never wrote why I thought JSON roundtripping would be a good goal:
|
JSON.stringify(record)
keys/values of records and as entries in tuples
fix #39