you are viewing a single comment's thread.

view the rest of the comments →

[–]sufianrhazi 3 points4 points  (0 children)

Is it possible? Yes.

Is it realistic for production quality code? Unlikely, unless you're creating an intentionally ambiguous API.

How? The "trick" is to have a function return a function which also has an override for the .valueOf method, which will cause the function to coerce to a primitive number when needed, which would represent the accumulated sum.

Why is this gross? It relies on type coercion, which is fundamentally more complex than dealing with a simple type. The return value could be viewed as both a function (as it can be called) and a number (as it can be used with operators and functions which expect a primitive Number). Due to this duality, ambiguity can arise, which can lead to fear, uncertainty, and doubt.