Created
April 13, 2021 05:31
-
-
Save hackape/9d2c97acf24ba419de6748bc9339bbd6 to your computer and use it in GitHub Desktop.
JS. fake infix overload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** might not be a good idea | |
* but it's fun | |
* you can overload infix operator in JS by overriding `.valueOf()` method. | |
The following binary operators coerce: | |
+ - * / % | |
& | ^ << >> >>> | |
< <= > >= | |
*/ | |
let anchor | |
function getActorRef() { | |
const ref = Object.create({ | |
valueOf() { | |
anchor = this | |
}, | |
}); | |
ref._pid = 42 | |
return ref | |
} | |
function send(dest, msg) { | |
console.log('dest:', dest) | |
console.log('msg:', msg) | |
anchor = null | |
} | |
Object.prototype.valueOf = function valueOf() { | |
send(anchor, this) | |
return this | |
} | |
function test() { | |
const pid = getActorRef() | |
// overload GE operator to be SEND operator | |
pid <= { type: 'hello', payload: 'world' } | |
} | |
test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment