The Tail Operator '#' enables clean typechecked builders with minimal language changes.

FeepingCreature feepingcreature at gmail.com
Fri Jan 25 07:24:10 UTC 2019


A problem that's come up before is how to specify builders in a 
typechecked manner. In my language syntax experiment, Neat, I'd 
copied Haskell's '$' operator, which means "capture everything to 
the right of here in one set of parens", in order to avoid 
writing parentheses expressions:

     foo(2 + 2);
     // becomes
     foo $ 2 + 2;

But what if we wanted the *opposite*? What if we wanted to 
capture everything to the *left* of an operator in one set of 
parens? Enter the tail operator:

     (2 + 2).foo;
     // becomes
     2 + 2 #.foo;

Why is this useful?

Well, we've had the debate before about struct initializers. The 
irksome thing is that the language is *almost* there! We can 
already write function calls as assignments:

     foo(5);
     // becomes
     foo = 5;

The main problem is that there's no way in the language to chain 
assignments together in that syntax. If we could, we could 
generate very clean looking builder expressions. But

     (((Foo.build
       .a = 3)
       .b = 4)
       .c = 5)
       .value;

Just doesn't look good and isn't as easy to modify as it should 
be either.

But with the tail operator, this becomes:

     Foo.build
       #.a = 3
       #.b = 4
       #.c = 5
       #.value;

Which is pure, typechecked, easy to extend and still readable.

What do you think?


More information about the Digitalmars-d mailing list