DIP6: Attributes

Sergey Gromov snake.scaly at gmail.com
Tue Aug 4 08:11:15 PDT 2009


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.



More information about the Digitalmars-d mailing list