function literals cannot be class members

Mehrdad wfunction at hotmail.com
Wed Jul 20 00:03:26 PDT 2011


On 7/18/2011 6:21 AM, Steven Schveighoffer wrote:
>> I'm actually still confused at why _functions_ should be passed as 
>> template parameters
>
> The beauty of alias parameters <snip>

... the beauty?

Please excuse me my tone becomes a bit rant-y, but I was asking _why_, 
and the reason is just... beauty? IMHO, it's getting pretty darn ugly...

Templates are just that: *templates*. They're meant to avoid 
__duplication of code and/or unnecessary variation in code__, by 
providing a common style (i.e. a common *template*). Metaprogramming, 
conditional compilation, etc. are great uses of templates, since they 
avoid unnecessary duplication of code.
But using them _in lieu_ of delegates is kind of pointless -- delegates 
*already* avoid duplication of code, and aliases don't solve any 
problems here. (I'm guessing you disagree, in which case: Can you 
provide an example of a problem that they actually solve, aside from 
their "beauty"?)

Sure, they're beautiful, but they're ugly at the same time: they don't 
let the code describe itself. i.e. when you say
     void Foo(alias F)() { ... }
the reader has **no idea whatsoever** what kind of "thing" F is (string? 
delegate? data type?), and it gets cryptic fast. Sure, you can document 
it, but isn't the whole point to let the code document itself? Not only 
that, it becomes absolutely /painful/ to write completely correct 
conditions for a template like this, when you can have multiple kinds of 
parameters.

If you want to be able to pass multiple parameter types, then why not 
just either use overloading or, if you're really concerned about 
duplication, let the argument type be specified as a template? That 
would remove the duplication issue.
If you're worried about simple things like "a < b", then it's because we 
shouldn't be using a string in the first place! It's painful to write
     foo(delegate(a, b) { return a < b; })
but that's an easily solvable solution: Just support C#'s syntax of 
lambdas and you instead get:
     foo((a, b)  =>  a < b);
which is cleaner and easy to type, and which avoids using strings for 
code. Or you can just predefine it somewhere as a lessThan template, and 
pass it as an argument. Either way, no strings.
In any case, this shouldn't be an excuse for using templates -- if 
typing something is hard, simplifying the syntax is the answer (I'm all 
for C# lambdas...), not using the template hammer to solve everything.


More information about the Digitalmars-d mailing list