DEV Community

Discussion on: βž• Operator Overloading in JavaScript, yes you can!

Collapse
drsensor profile image
DrsEnsor

I wonder if it still work for vector operation πŸ€”

Collapse
adam_cyclones profile image
Adam Crockett Author

I thought about it again, I remember the problem with vector 3 or vector 4 sizes, you need to retain all 4 or 3 values and represent them as a type in such a way that the plus can be used, eg string and number, and then re translate them back to vectors again, it sounds like a fun challenge, let me know if you try it

Collapse
adam_cyclones profile image
Adam Crockett Author

It has been a long time but I think I have a vector implementation

Collapse
drsensor profile image
DrsEnsor • Edited on

How do you detect what kind operation it is (e.g +, *) by just listening on valueOf()?

Thread Thread
adam_cyclones profile image
Adam Crockett Author

Here we go, the very technical details
2ality.com/2011/12/fake-operator-o...

Thread Thread
adam_cyclones profile image
Adam Crockett Author

Details of that are here 2ality.com/2011/12/fake-operator-o...

Thread Thread
drsensor profile image
DrsEnsor • Edited on

Unfortunately that technique is bound to fail when each expressions executed asynchronously. I think there is need a way to mark or even better get the value of the right hand side of the operator πŸ€”

Thread Thread
adam_cyclones profile image
Adam Crockett Author

I'm afraid at that point in the article, I reached my limit of comprehension, my attention span crumbled into little pieces of biscuits 😎. I did stumble across a method using Proxy, I know the reality breaking power of Proxy, so I suspect it may be the way to go. If you try to solve this one, I would like to see the results. But I can tell you that NPM does have this problem solved in the form of a few packages, some compiling and some runtime, best of luck my friend

Thread Thread
drsensor profile image
DrsEnsor

At least it doesn't do any string operation. Another approach is to use either tagged-template-literal or JSX but it hit perf quite bad since it need to translate math ops that is in string into the real one (e.g "+" -> +). This is a big no for gamedev. The solution is to use babel-macro but it slow down the devserver since it depend on babel. I wish esbuild support macro or there is a go esbuild-plugin for this.
Sorry for the rambling. Best luck to us πŸ˜ƒ