The syntax of sort and templates

zakk via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 26 02:59:26 PDT 2017


Hello everyone,

I just started using D and I am a bit puzzled by the syntax of 
the sort function is std.algorithm.sorting, which is

sort!(comparingFunction)(list)

where comparingFunction is often a lambda expression. For 
instance in the Wolfram Language the equivalent function is

Sort[list,comparingFunction]

My questions are:

1) Why is D making using of the binary ! operator, which as far 
as I understand introduces a template?

2) Why is a template needed here?

3) It seems to me like the argument passed to the template is a 
lambda expression. I only know about templates taking types as 
argument. What's going on?

Many thanks!


More information about the Digitalmars-d-learn mailing list