Skip to content

Instantly share code, notes, and snippets.

@hackape
Created April 13, 2021 05:31
Show Gist options
  • Save hackape/9d2c97acf24ba419de6748bc9339bbd6 to your computer and use it in GitHub Desktop.
Save hackape/9d2c97acf24ba419de6748bc9339bbd6 to your computer and use it in GitHub Desktop.
JS. fake infix overload
/** 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