how to define infix function

KnightMare black80 at bk.ru
Thu Apr 11 10:29:08 UTC 2019


On Saturday, 2 June 2018 at 22:01:02 UTC, Ali Çehreli wrote:
> On 06/02/2018 02:44 PM, greatsam4sure wrote:
> > is it possible to define infix function in D
> > 3.min(5)// 3: where min is a function, works in D
> > 3 min 5 // does not work.
> This is called universal function call syntax (UFCS) in D.
> Ali

UFCS is not same as infix functions.
infix allow to u write code like:

1)for (i in 0 to 10 step 2) // `in`(can be keyword too), `to` and 
`step` can be some functions that change range or something
2) auto shiftedRes = someVar shr 13; // `shr` is infix function 
too

u can use infix function with 1arg without any parentheses.
why this need? how it can be useful? look at Kotlin lang.
pure functional programming.
it can be useful for code looks like LINQ(.NET): DB, UI...

and I think UFCS should be improved too: function with 1arg can 
be written without parenthesis too. for example
some declarations:
class Task<T> { .. };
Task<Buffer> asyncRead( File file ) { .. }
T await!(T)( Task<T> task ) { .. }
we can use await like:
auto buf = asyncRead( file ).await();
or
auto buf = await( asyncRead( file )); // I like spaces between 
fn-names and args
but more clear IMO:
auto buf = await asyncRead( file ); // less parenthesis

more improvements:
1) see Kotlin passing lambda as last parameter
https://kotlinlang.org/docs/reference/lambdas.html#passing-a-lambda-to-the-last-parameter

2) Kotlin/when with pattern matching. dont need change current 
`switch` instruction. and we can make `when` as expression (look 
Kotlin samples)

3) scope functions 
https://kotlinlang.org/docs/reference/scope-functions.html
with(button) { text = "hello"; background = Colors.Yellow; }
text & background are props of some Button class for instance 
button. {} is a lambda as last arg


More information about the Digitalmars-d-learn mailing list