delegate !is null

Saaa empty at needmail.com
Sun Sep 6 15:54:47 PDT 2009


"Steven Schveighoffer" <schveiguy at yahoo.com> wrote in message 
news:op.uzqxxo1neav7ka at localhost.localdomain...
> On Fri, 04 Sep 2009 14:33:12 -0400, Saaa <empty at needmail.com> wrote:
>
>> class C
>> {
>>   private int i;
>>   int method()
>>   {
>>     return i;
>>   }
>> }
>>
>> class D
>> {
>>   private int delegate(void) _deleg;
>>   this(int delegate(void) d)
>>   {
>>     _deleg = d;
>>   }
>>   void write()
>>   {
>>     if(_deleg !is null)
>>     }
>>       writef(_deleg());
>>     }
>>   }
>> }
>> C c = null;
>> D d = new d;
>> d.function(c.method());
>> //This fails, as method is not availible for null.
>> d.function({return c.method();});
>> //This works but now I can't check whether c is null or not.
>> d.write(); //will fail.
>>
>> Any suggestions?
>
> Maybe you could rephrase your question in english.  I can't really 
> understand what you are trying to do with this code.
>
> i.e. "I want to be able to tell whether a delegate is null or not, how do 
> I do that".  But you do that just like you said -- dg !is null.
>
> -Steve

(unexpected visit this weekend)
Erm, like this.. ?
I'd like to set D's delegate to a method which is not yet available (like 
c.method).
I solved this by encapsulating the method within a function literal, but I 
also need to know whether
the method is available or not when calling the delegate.
I could do this by making the function literal include the null-checking 
code, but is there maybe a better solution to this problem?
The delegate is supposed to change a variable within the D class.
Hope you understand it :)





More information about the Digitalmars-d-learn mailing list