Love the video and your consistently amazing content. It's a shame that there aren't more videos about even slightly advanced programming topics. Thank you for your great work. I am not a huge fan of method overloading, but I really really miss operator overloading. Being forced to use method chaining when using Big.js, for example, just feels kind of clumsy.
Operator overload is possible in smalltalk. Keyword parameters help quite a bit. You can a actually define your own operators such as+++ though this has it's trade offs
Amazing content .. personally I prefer not to overload if possible but it is an important principal of oops. My worst (or best) work was writing 15 overloaded methods for a java package in a company I worked for. If I have to do that again, i would prefer named parameters in kotlin. Named parameters in kotlin and now php8 is amazing for overloading methods without duplicating efforts for programmers.
Yeah I see where you're coming from. I think overloading is like many other kinds of language feature in that it can be used in some really counter productive ways.
I was thinking if there was some good way of annotating existing functions with types, without having all that extra machinery like: function add( a /* number */, b /* number */) { .. } and then using Function.prototype.toString to extract those annotations, but that extra string -> type conversion will be quite hard to manage as well. Great video as always!
Thanks Otto - I've played around with this kind of thing before. If you're parsing the function strings yourself it can get quite hairy now that JS allows default values to be assigned. Still, if you expect the users to "play by the rules", and you don't care about modeling more complex data types, you could probably get it to work quite nicely! One of the most insane projects I've ever seen doing this kind of thing is ganja js (github.com/enkimute/ganja.js/). It parses function strings and allows for operator overloading, repurposing the scientific notation for complex numbers, and a bunch of other crazy stuff.
Great video! My biggest problem here is the API: you're inventing class syntax again. I would love to use existing classes but for some methods (static or not) just define them like you do: class Vector3 { constructor(...) {...} add = overload((V3, V3Type) => [ { args: [...], fn: function(...) {...} }, { ... } ]) }
Thanks Jussi. I agree that the syntax is not ideal. The trouble with the method you propose is that the methods will be per instance rather than per class - so if you have 1000 instances you'll have the same number of copies of the method in memory.
why not use a combination of arguments.length and the typeof or instanceof operators to dynamically run the commands necessary for each respective situation?
this is the reason webpages are so stupidly slow in relation to the useful computations needed to perform some actions. Just to be able to pass different arguments to the function we execute 300 lines of code, some of which in 2 external libraries...
Kind of annoys me that you collapse the argauments into methodArgs specifically so you can expand then re-collapse them immediately afterwards. Passing around an array of arguments would have been cleaner, especially when JS provides a magic variable for that(arguments).
Method overload doesn't buy you much in clarity of behavior. Parameters were never meant for inheritance abuse. But at least this is addressed in the video
Cool stuff but unfortunately this approach is one big V8 deopt in terms of performance. It won't play nice with hidden classes and inline caching (you could test with V8 Indicium). And an O(N) loop every method call isn't great, though imo the best approach is not paying any runtime implementation cost to begin with...
Typescript doesn’t address what this video is about Typescript explicitly avoids extending the runtime type system which is required for dynamic dispatch