struct object sub-type

I propose the Struct class or the struct keyword.
Any subclass of Struct or any struct class should be of the struct type and final, when the struct object exits the constructor it should be sealed, and have value semantics.

example:

class Point extends Struct{
    x=0;
    y=0;
    constructor(x,y){
        this.x=x;
        this.y=y;
    }
}
//or
struct Point{
    x=0;
    y=0;
    constructor(x,y){
        this.x=x;
        this.y=y;
    }
}
assert(typeof new Point(0,0) === 'struct')
assert(new Point(0,0) === new Point(0,0))
assert(new Point(1,0) !== new Point(0,0))
new Point(0,0).z = 0; //fails

this will be a great tool for memory optimization in javascript

Proposals need to start with a problem to be solved - suggesting the solution is often inappropriate until the problem is understood and supported. What problem does this attempt solve?

Additionally, claims about optimization opportunities are often found to be (unintuitively) false by engine implementers, so I'd suggest avoiding such claims unless you are one. (i am not one either, to be clear)

1 Like

it should just be a way to make objects be passed by value instead of by reference so that you don't have code accidentally modifying some object it shouldn't touch if the function was suppoded to copy just one value but you forgot to but it adds it.
and to also decrease gc and memory access time

Currently at stage 2

Everything in JS is pass by value, technically - it's just that objects are like a by-value pointer you can't avoid dereferencing. If you want an object to not be mutated, you can use Object.freeze() on it.

It would be nice to have value semantics on certain types of objects

any reason it got stuck at Stage 2 ??? it's been around for a while and it would be a great feature to have, especially the SharedStruct part which might finally put an end to the SharedArrayBuffer madness GitHub - tc39/proposal-structs: JavaScript Structs: Fixed Layout Objects Β· GitHub