DIP6: Attributes

Ary Borenszweig ary at esperanto.org.ar
Tue Aug 4 08:16:48 PDT 2009


Sergey Gromov wrote:
> Tue, 04 Aug 2009 11:58:27 -0300, Ary Borenszweig wrote:
> 
>> Sergey Gromov wrote:
>>> Tue, 04 Aug 2009 22:46:22 +1000, Daniel Keep wrote:
>>>
>>>> Sergey Gromov wrote:
>>>>> Tue, 04 Aug 2009 17:22:50 +1000, Daniel Keep wrote:
>>>>>
>>>>>> Don wrote:
>>>>>>> ...
>>>>>>>
>>>>>>> A question: in C#/Java, can you have annotations on function pointer and
>>>>>>> delegate declarations?
>>>>>>>
>>>>>>> void foo( int delegate(int) pure dg) {
>>>>>>>   ...
>>>>>>> }
>>>>>>> What would this look like with annotations?
>>>>>> Well, Java doesn't HAVE delegates and C# doesn't (AFAIK) allow you to
>>>>>> define them inline; they have a special declaration syntax that can't be
>>>>>> used in an expression.
>>>>> C#:
>>>>>
>>>>> List<int> ls;
>>>>> ls.Sort((x, y) => y - x);
>>>>>
>>>>> or
>>>>>
>>>>> ls.Sort((x, y) => { int a; a = y; a -= x; return a; });
>>>> That's not a delegate type, that's a delegate literal.
>>> Sorry, you said: "C# doesn't ... allow you to define them (delegates)
>>> inline".  Delegate literal *is* an inline definition of a delegate.
>>> What you say now is that C# doesn't allow to define a delegate type
>>> inside a function which is definitely true and is very annoying.
>> Have you seen the Func delegates? They are exactly for that.
>>
>> The above example would be:
>>
>> void foo(Func<int, int> dg) { ... }
> 
> Func defines a delegate with one argument and a non-void return.  There
> are lots of others like Action, Predicate, Comparator etc. which you
> must either remember or look up every time you need a specific delegate
> signature.

Ah, I didn't know you can't do something like Func<void, int>. So there 
could just be two things to remember: Func and Action.

Also you have Func<T1, T2, TResult>, Func<T1, T2, T3, TResult>, so it's 
not just one argument.

I just tried casting one delegate type to another one with the same 
signature and it doesn't work. So you are right, you need to remember 
Predicate, Comparator, etc. :-(



More information about the Digitalmars-d mailing list