Closed ljharb closed 1 month 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?
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.
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.
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.
Considering this issue addressed. To recap:
For unshared structs, there's no brand check as they are "just" sealed ordinary objects. Since struct instance methods are non-generic, all struct instance methods may be considered brand checks for instances of that particular struct.
For shared structs, the spec draft includes a Reflect.canBeShared
, which returns true for shared structs and primitives. This can be used in conjunction with typeof foo == 'object'
to see if something is a shared struct in particular.
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?