Skip to content
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

Predictability goals are confusing #36

Open
Zekfad opened this issue Aug 29, 2020 · 3 comments
Open

Predictability goals are confusing #36

Zekfad opened this issue Aug 29, 2020 · 3 comments

Comments

@Zekfad
Copy link

Zekfad commented Aug 29, 2020

The meaning of operators on existing objects shouldn't be overridable or monkey-patchable, both for built-in types and for objects defined in other libraries.

This one makes no sense, because it's already possible to do something like

Number.prototype.valueOf = () => { return 'smth bad',-1; }

It should not be possible to change the behavior of existing code using operators by unexpectedly passing it an object which overloads operators.

This one again makes no sense, because as I understand that means you can't use operator overloads with non overloaded objects, but why? It's that the reason for op. overloads, to make fully customizability behavior or object manipulation during operator actions?

I guess i just misunderstood most of that two rules, could you please clarify it?

@rihok
Copy link

rihok commented Sep 4, 2020

I'm also unsure why this is an issue here, but not in the rest of JavaScript. To me, this rule seems to increase the complexity of the solution in terms of legibility and usability (having to add with operators, and having to use a separate construct that is external to the object).

Unless I'm misunderstanding, it's this rule that prevents us from having a much cleaner and easier to understand API that would look more like this:

class Vector3 {
    //...
    operator * (a, b) {
        return new Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
    }

    operator == (a, b) {
        return a.x === b.x && a.y === b.y && a.z === b.z;
    }

    operator > (a, b) {
        //...
    }
}

const myObject = {
    [*]: (a, b) => a.value * b.value,
    [>]: (a, b) => a.value > b.value
}

myObject.defineOperator("-", (a, b) => a.value - b.value)

And if you need to pass without operators:

Object.raw(myObject);

@CristianPi
Copy link

The same for us We want to change the behavior of old program, that's one of the point of meta programming.

@Haringat
Copy link

Haringat commented May 23, 2022

@rihok Option 2 could be a bit of a hazard to parse but I like option 3. Maybe replace the string with a unique well-known symbol and make it Object.defineOperator(myObject, ...)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants