Optimizing delegates

Ary Borenszweig ary at esperanto.org.ar
Sun Dec 19 15:25:00 PST 2010


On 12/19/2010 07:54 PM, Andrej Mitrovic wrote:
> There's an isDelegate template function in std.traits, but I think
> there's some kind of bug there. I've reported it and I'm waiting for
> answers.
>
> Otherwise there are other functions you can use, e.g.:
>
> isSomeFunction(T), isCallable(T), is(FunctionTypeOf(T) == delegate)..
>
> On 12/19/10, Michel Fortin<michel.fortin at michelf.com>  wrote:
>> On 2010-12-19 15:30:50 -0500, Ary Borenszweig<ary at esperanto.org.ar>  said:
>>
>>> But that is a template:
>>>
>>> int foobar2(int delegate(int x) f)() {
>>> }
>>>
>>> It's a template that doesn't work because I have to write it in a
>>> different way. Sorry, I tried many different template constraints and
>>> none of them work.
>>>
>>> I tried these:
>>>
>>> int foobar2(alias f)()
>>>     if (typeof(f) == typeid(int delegate(int x)))
>>>     if (is(typeof(f) == int delegate(int)))
>>>     if (is(typeof(f) == delegate))
>>>
>>> What do I have to write to make it work?
>>
>> 	int foobar2(alias f)()
>> 		if (is(typeof(f(int.init)) == int))
>>
>> On the plus side, it'll not only work with delegates but also with
>> regular functions, template functions, and objects with an opCall
>> member.
>>
>> If you absolutely want to limit it to a delegate, you can try this:
>>
>> 	int foobar2(alias f)()
>> 		if (is(typeof(&f) == int delegate(int)))
>>
>> --
>> Michel Fortin
>> michel.fortin at michelf.com
>> http://michelf.com/

What I don't like about that is that it's not consistent. I can write:

int foo(int x)() {
}

but I cannot write:

int foo(int delegate(int) x)() {
}

Well, I can, and the compiler gives me an error saying "No, you can't do 
that". I have to dig std.traits code or use one of those killer 4 level 
nested parenthesis to tell the compiler "this is a delegate". With what 
I'm telling it the compiler already sees I want to define a template 
that accepts a delegate. Don't you all think it's more consistent this 
way? I know there is a way to do it, but having to learn 10 ways to 
write things depending on what type I'm going to use is very time consuming.

And before you tell me "In ruby there are 10 different ways to do X", 
that's fine, because all of them work. That's consistency. No matter how 
I express myself to the compiler/language, it works. But if the compiler 
starts telling me "Oh, you know, for this type I don't like this syntax, 
please use another one"...

Same goes with "a>b". It's not consistent. It sometimes doesn't work.


More information about the Digitalmars-d mailing list