You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 3, 2025. It is now read-only.
Repository navigation
This repository was archived by the owner on Apr 3, 2025. It is now read-only.
Use a mathematical definition of operators, decoupled from class definitions #44
In mathematics, an operator is essentially a function that maps elements from one set to a single element in another (or the same) set [mathematicians please excuse my over-simplification]. The operator is not a property of any set, but the definition of the sets is a prerequisite to the definition of the operator.
Taking this kind of direction in javascript, an operator overload would not be defined as a property of a class, but as a function that checks its arguments' types and performs the appropriate calculations based on that.
A key advantage of this is that a user could define an operator over two classes or modules from different sources, that would not be aware of each other by default:
importComplexfrom'complex-numbers';importVectorfrom'vectors';const[operator*]=function(left,right){if(leftinstanceofComplex&&rightinstanceofVector){const[complex,vector]=[left,right];returnnewVector(vector.elements.map(vectorElement=>complex.multiplyBy(vectorElement)))}// Include some way of falling back to default either explicitly or implicitlyreturnsuper(complex,vector);}constcomplex=Complex('i + 5');constvector=Vector([0,1,2]);constresult=complex*vector;// Result is Vector([0, Complex('i+5'), Complex('2i+10')])console.log(result);
This kind of implementation probably would require that operators work with scopes and closures in the same way that const or let variables are, in order to stop overloading affecting other areas of the code unintentionally. But I don't think that's a problem (in fact it could be considered improving code clarity anyway).
I feel that operator overloading should not be defined in such absolute terms. Say you were using two libraries that overloaded the same operator, there would be a conflict.
I believe that a better approach could be too define an "operator" for given types, and the operator function would only be applied if the arguments matched the defined types.
Such approach would have a "composition over inheritance" model, where as in your proposed syntax, operators would override their parents, and run imperative code.
The operator definition could be preceeded by 'export', too make the operator available to other modules.
importComplex,(Complex*any)from'complex-numbers'importVectorfrom'vectors'exportoperator(Complexcom*Vectorvec){// we can do 'com * element' since we imported the (Complex * any) operator from complex-numbersreturnnewVector(vec.elements.map(element=>com*element))}
Additional syntax could be use to mark an operator as comutative. operator (Complex com comutative* Vector vec) { ... }
This syntax could also allow for ternary operators.
Yes, ultimately this is the same idea I was trying to portray of separating operator definition from type definition.
There are other questions still to solve such as how to scope the operator, whether to enable duck-typing instead of instanceof checking, etc. But I think this is a good direction.
Yes, ultimately this is the same idea I was trying to portray of separating operator definition from type definition.
There are other questions still to solve such as how to scope the operator, whether to enable duck-typing instead of instanceof checking, etc. But I think this is a good direction.
the original proposal proposes a with operator from 'module' syntax
I believe this could be used here too. So instead of importing the operator, you'd declare it within the scope with with
Activity
jvcmarcenes commentedon May 20, 2021
I feel that operator overloading should not be defined in such absolute terms. Say you were using two libraries that overloaded the same operator, there would be a conflict.
I believe that a better approach could be too define an "operator" for given types, and the operator function would only be applied if the arguments matched the defined types.
Such approach would have a "composition over inheritance" model, where as in your proposed syntax, operators would override their parents, and run imperative code.
This is a proposed syntax:
The operator definition could be preceeded by 'export', too make the operator available to other modules.
Additional syntax could be use to mark an operator as comutative.
operator (Complex com comutative* Vector vec) { ... }This syntax could also allow for ternary operators.
And for new operators.
PaulKiddle commentedon May 20, 2021
Yes, ultimately this is the same idea I was trying to portray of separating operator definition from type definition.
There are other questions still to solve such as how to scope the operator, whether to enable duck-typing instead of instanceof checking, etc. But I think this is a good direction.
jvcmarcenes commentedon May 20, 2021
the original proposal proposes a
with operator from 'module'syntaxI believe this could be used here too. So instead of importing the operator, you'd declare it within the scope with
with