@@ -221,6 +221,95 @@ functions.time = function() {
221221
222222</details>
223223
224+ ### Operator Overloading
225+
226+ <details>
227+
228+ <summary>Click to read about operator overloading</summary>
229+
230+ In Code Grid formulas, infix operators (such as ` + ` ) are compatible with the
231+ same types as the corresponding JavaScript operator. For example, it is equally
232+ valid to do ` " x" + " y" ` or ` 3 + 2 ` in formulas, since addition works on both
233+ strings and numbers in JavaScript. Using the same infix operator (` + ` ) for
234+ different operations on different types is called "operator overloading."
235+
236+ Code Grid allows users to extend infix operations to work on more complex types
237+ through advanced operator overloading. To make a type use a custom operation for
238+ an infix operator, define a method with the same name as that operator. That's
239+ it. For binary operations, the implementing method should take one argument. For
240+ unary operations, the method should take no arguments.
241+
242+ For example, we could implement vectors that support element-wise addition:
243+
244+ ` ` ` javascript
245+ class Vector {
246+ constructor (a ) {
247+ this .elements = a;
248+ }
249+
250+ toString () {
251+ return " <" + this .elements .join (" , " ) + " >" ;
252+ }
253+
254+ [" +" ](v ) {
255+ return new Vector (this .elements .map ((x , i ) => x + v .elements [i]));
256+ }
257+ }
258+
259+ functions .v = (... a ) => new Vector (a);
260+ ` ` `
261+
262+ Then, the following would be a valid Code Grid formula that would evaluate to
263+ ` < 1 , 2 , 3 > ` , even though adding vector objects in JavaScript would throw an
264+ error:
265+
266+ ` ` `
267+ = v (0 , 3 , 1 ) + v (1 , - 1 , 2 )
268+ ` ` `
269+
270+ We could also implement overloading of the unary ` ~ ` operator to switch the sign
271+ of all vector elements by adding the following method to the ` Vector` class:
272+
273+ ` ` ` javascript
274+ class Vector {
275+ // ...
276+
277+ // ~<1, -1, 3> => <-1, 1, -3>
278+ [" ~" ]() {
279+ return new Vector (this .elements .map (x => - x));
280+ }
281+
282+ // ...
283+ }
284+ ` ` `
285+
286+ Consider operations between different types. For example, if we want to
287+ implement vector-scalar subtraction, we will need to handle ` < vector> - scalar`
288+ as well as ` scalar - < vector> ` .
289+
290+ When evaluating an infix operation ` x op y` , Code Grid first tries ` x .op (y)` ,
291+ then ` x .op .forward (y)` , then ` y .op .backward (x)` , finally falling back on the
292+ default operator implementation if nothing else works. In this example, we will
293+ implement ` < vector> - scalar` in the ` forward` method, and ` scalar - < vector> `
294+ in the ` backward` method:
295+
296+ ` ` ` javascript
297+ class Vector {
298+ // ...
299+
300+ [" -" ] = {
301+ // <v> - s
302+ forward : (s ) => new Vector (this .elements .map (x => x - s));
303+ // s - <v>
304+ backward : (s ) => new Vector (this .elements .map (x => s - x));
305+ }
306+
307+ // ...
308+ }
309+ ` ` `
310+
311+ </details>
312+
224313# How Code Grid Works
225314
226315## Code Table of Contents
0 commit comments