Symbol.primitiveSet

Possible use case:

let primitiveLike = {
     value: 0,
    [Symbol.toPrimitive](hint) {
        return hint == "string"
            ? String(this.value)
            : hint == "number"
                ? Number(this.value)
                : this.value
        ;
    },
    [Symbol.primitiveSet](value) {
        this.value = value;
        return true;
    }
}

primitiveLike++;

console.log(Number(primitiveLike)) // 1

This doesn't work when the object decleared as constant value.
Do you think this will be needed?

Having a “primitiveLike” by itself seems like a massive problem - objects have a distinct identity, primitives do not, and while coercing between the two is possible, conflating them is highly undesirable.

1 Like

Maybe the naming was inappropriate, but I think it's necessary to have the ability to determine the behavior when reassigned. I also don't think it's appropriate to hack the behavior of let, and I think it's better to have it work only with const.

Assignments are to bindings - variables - not objects, and it just doesn't make any sense to me to have the way assignment works suddenly change and become fallible.

2 Likes