3 ms·
I like C#'s approach where a static function can be called as a method by adding the "this" keyword to the first parameter in its declaration. This makes it op
by Pxtl 11y ago
I like C#'s approach where a static function can be called as a method by adding the "this" keyword to the first parameter in its declaration. This makes it opt-in, which is annoying for legacy code. All collections got a fantastic map/filter/fold system bolted on through this mechanism, and as Bjarne noted the dot notation is great for chaining calls.
So in C#, to declare an fn as an "external method" you say:
static void MyFunc(this MyClass myObj, int someParam){doStuff();}
Which can be called as
var foo = new MyClass();
foo.MyFunc(5);
The downside is that method not found errors become much more complicated and it's not obvious to the user whether a method is coming from a different package or not.