tc39 / proposal-structs

JavaScript Structs: Fixed Layout Objects
http://tc39.es/proposal-structs/
622 stars 11 forks source link

how can i identify a struct, and whether it's shared? #23

Closed ljharb closed 1 month ago

ljharb commented 7 months ago

My understanding is that a struct is just a sealed object with null, or another struct, as a [[Prototype]] (shared structs may have an object with methods as their [[Prototype]]).

Given some x, how can i determine if it's an unshared struct, or a shared struct, vs a non-struct?

syg commented 7 months ago

Because [[Prototype]] chains are immutable for both unshared and shared structs, you should use instanceof for checking instances of specific classes.

For unshared structs, there's nothing really distinguishing it from a sealed object with non-generic methods.

For shared structs, it might be worth adding a brand check, or at least a general isShared predicate to check for things being shared and therefore have the "can't point to unshared things" restriction.

What's the use case?

ljharb commented 7 months ago

Debugging, logging, test frameworks - anything where a value needs to be described in a robust way.

For unshared structs, is there any special behavior they have, or is it literally just syntax sugar for something else? Can I perfectly replicate an unshared struct without the syntax?

A predicate would be perfectly fine, for either/both.

syg commented 7 months ago

Debugging, logging, test frameworks - anything where a value needs to be described in a robust way.

But what's the use case for debugging, logging, and testing unshared structs differently than sealed objects? I can see the need to do it for shared structs, but there I see it as a need for all shared objects.

For unshared structs, is there any special behavior they have, or is it literally just syntax sugar for something else? Can I perfectly replicate an unshared struct without the syntax?

You'd have to write some custom constructors but yes.

ljharb commented 7 months ago

In that case I'd only require a predicate for shared ones, even though it'd still be nice for an unshared one. I totally agree that it applies to all shared objects, but in order to have the ability to help the user find the thing in question in their code, i'd still need to know that it's a shared struct, specifically.

syg commented 1 month ago

Considering this issue addressed. To recap: