r/ProgrammerTIL • u/roberestarkk • Feb 11 '19
Other Language [DLang] TIL DLang is one of only TWO programming languages that feature Uniform Function Call Syntax (chaining dots is equivalent to nesting parens)
Uniform Function Call Syntax (UFCS) is such a cool concept, I'm really looking forward to all the compiler errors in every other language when I fall into the habit of using it!
This feature is especially useful when chaining complex function calls. Instead of writing
foo(bar(a))It is possible to write
a.bar().foo()Moreover in D it is not necessary to use parenthesis for functions without arguments, which means that any function can be used like a property
https://tour.dlang.org/tour/en/gems/uniform-function-call-syntax-ufcs
Disclaimer: I am blatantly assuming there're only two, based on my extensive research (this one Wiki article), evidence to the contrary is welcome!
My latest 'real-world' use of this system involves chaining .split calls to whittle down an input string from AdventOfCode because I'm too lazy to implement proper pattern matching and extraction, though that is next on my list of things to look into.
void main(string[] args)
{
getInput.split("\n").map!(a => new LogLine(
a.split("]")[0].split("[")[1],
a.split("] ")[1]
)).sort!((a,b) => a[0] > b[0]).each!
(a=>a.convertToString.writeln);
}
string getInput() {
return `[1518-09-19 00:42] wakes up
[1518-08-06 00:16] wakes up
[1518-07-18 00:14] wakes up
[1518-03-23 00:19] falls asleep
[1518-10-12 23:58] Guard #421 begins shift
...I honestly can't imagine the pain of having to do that in the nesting style. It'd need several interim variables of various types just to stay remotely legible!
Although frankly I don't even know whether this code works (specifically the sort! call), it's past my bedtime so no more debugging, but dangit I learned it today I'ma post it today!
5
u/[deleted] Feb 11 '19
Assuming that
ais a string, your example works in basically every language with classes becausesplitreturns a new object. The function syntax has nothing to do with it.a.split("]")[0].split("[")[1]is valid Python, Java, Go, and C++.The syntax feature is cool and all, but you need an example that showcases it.
2
u/smthamazing Feb 11 '19
While your example doesn't actually show UFCS, I love this feature of D. UFCS in D, trait methods in Rust, extension methods in C# are often better alternatives to inheritance.
1
u/fridsun Feb 17 '19
As a Haskeller I prefer composing the functions together then apply it in one go /s
3
u/[deleted] Mar 03 '19
I don't see the difference, to be honest.
foo(bar(a))a.foo.barfoo( bar(a) )The goal with code is to always be clear what your intent is and to ensure that someone can read the code. I would much rather continue to use
a.foo().bar()to be clear that the members are not properties andamay or may not be an object.I would be hesitant to use the UFCS form because I don't want
ato be mistaken for an object, when it may not be. It actually makes some sense to allow this behavior because it is essentially how Python works with methods anyway. If a function accepts a type, then the compiler should know to inject the variable into the function into that parameter in a form of built-in currying.This will likely be added to other languages and we'll get to see how useful it is.
1
u/MCRusher Jun 05 '19
I'm trying a sort of similar system,
Called as
Both call the same function and have the same result, there is no address taking or dereferncing goung on, the :: operator deduces the type of i and substitutes the type (I32) as the last namespace identifier of the function name, then passes i directly as the first element of this function.
0
u/badhombrez Feb 11 '19
but why would you?