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

Accessing the original operator when creating Operator #713

Open
Jamesernator opened this issue May 31, 2017 · 1 comment
Open

Accessing the original operator when creating Operator #713

Jamesernator opened this issue May 31, 2017 · 1 comment

Comments

@Jamesernator
Copy link

Jamesernator commented May 31, 2017

A pain point I hit really quickly with trying to use operator macro was trying to permit operator overloading using macros, but I quickly realised there doesn't seem to be any way to access the original operator, because even if I try to implement another operator which has the original it also gets replaced.

As an example:

operator + left 12 = (left, right) => {
    return #`(${ left })[Symbol.add](${ right })`
}

Symbol.add = Symbol('Symbol.add')

Number.prototype[Symbol.add] = function(other) {
    // Obviously this a no-go as the operator will get replaced
    return this + other
}

console.log(3 + 4)

So that doesn't work as all operators get replaced in all contexts which is fine, but then I tried adding a second operator to access the original:

operator $plus left 13 = (left, right) => {
    return #`${ left } + ${ right }`
}

operator + left 13 = (left, right) => {
    return #`(${ left })[Symbol.add](${ right })`
}

Symbol.add = Symbol('Symbol.add')

Number.prototype[Symbol.add] = function(other) {
    "use strict"
    return this $plus other
}

console.log(3 + 4)

But this still doesn't work because this $plus other gets replaced with this + other then replaced again with this[Symbol.add](other).


What'd be nice is if we could protect some code from being affected by macros:

Number.prototype[Symbol.add] = function(other) {
    #nomacro {
        return this + other
    }

    // Or maybe if we could exclude specific macros
    #nomacro + {
        return this + other
    }
    
    // Such blocks would be scopable as nearby as needed:
    return #nomacro + { this + other }
}

The syntax above is just for exposition, while not strictly necessary (I could just define a library of normal operators and import it) I think it would make operator a lot more usable.

EDIT: The library definition operators workaround I suggested won't work at all for lazy operators or special tokens like => that aren't really operators just tokens.

@gabejohnson
Copy link
Member

I would suggest defining Number.prototype[Symbol.add] in another file in the mean time.

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

2 participants