4 ms·
I was recently made aware of D's Uniform Function Call Syntax (UFCS) which is similar but not exactly the same: void sun(T, int); void moon(T t) {
by codethief 5y ago
I was recently made aware of D's Uniform Function Call Syntax (UFCS) which is similar but not exactly the same:
void sun(T, int);
void moon(T t)
{
t.sun(1);
// If `T` does not have member function `sun`,
// `t.sun(1)` is interpreted as if it were written `sun(t, 1)`
}
The nice thing here is that no placeholder like % is needed and it still works with functions that expect multiple parameters.
Docs:
https://dlang.org/spec/function.html#pseudo-member https://dlang.org/spec/function.html#pseudo-member
- egeozcan 5y agoI think Nim has the same, IIRC.
- davidkunz 5y agoThat is correct, they call it "method call syntax": https://nim-lang.org/docs/tut2.html#object-oriented-programming-method-call-syntax https://nim-lang.org/docs/tut2.html#object-oriented-programm...
- dtech 5y agoInteresting, it's also general enough to cover extension methods where languages C#, Kotlin and Scala have special constructs for.
- jkcxn 5y agoD’s way is far superior because as the caller you don’t have to worry about whether you’re calling a method or an external function, it looks the same. Not sure it would be possible to tack this onto a dynamic language like JavaScript though