Thread overview | |||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 04, 2009 delegate !is null | ||||
---|---|---|---|---|
| ||||
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? |
September 04, 2009 Re: delegate !is null | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | D should be like this :) class D { private int delegate(void) _deleg; private int _value; // this(int delegate(void) d) { _deleg = d; } void write() { if(_deleg !is null) } _value = _deleg(); // } writefln(_value); // } } |
September 04, 2009 Re: delegate !is null | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | On Fri, 04 Sep 2009 14:33:12 -0400, Saaa <empty@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
|
September 06, 2009 Re: delegate !is null | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | "Steven Schveighoffer" <schveiguy@yahoo.com> wrote in message news:op.uzqxxo1neav7ka@localhost.localdomain... > On Fri, 04 Sep 2009 14:33:12 -0400, Saaa <empty@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 :) |
September 08, 2009 Re: delegate !is null | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | On Sun, 06 Sep 2009 18:54:47 -0400, Saaa <empty@needmail.com> wrote:
> 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 :)
A delegate is a struct with a data pointer and a function pointer. You can access the individual parts via .ptr and .func (I believe). You can even change them via those properties.
does that help?
-Steve
|
September 08, 2009 Re: delegate !is null | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | "Steven Schveighoffer" <schveiguy@yahoo.com> wrote in message news:op.uzxs4wyreav7ka@localhost.localdomain... > On Sun, 06 Sep 2009 18:54:47 -0400, Saaa <empty@needmail.com> wrote: > >> 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 :) > > A delegate is a struct with a data pointer and a function pointer. You can access the individual parts via .ptr and .func (I believe). You can even change them via those properties. > > does that help? > > -Steve I did read that part. The problem lies more in that I'd like to point to something which is not there yet. In the code 'c.method()' is not there yet, as c is null. Maybe I should create a dummy object for c to point to in stead of null ? That way I point the delegate to the dummy method and ignore it as long as it is pointing to the dummy method :) The only drawback to this is that all objects I want to point the delegate to, need to somehow be convertable to the dummy type (interface/abstract class), meaning it will be less flexible. |
September 08, 2009 Re: delegate !is null | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | On Tue, 08 Sep 2009 09:22:30 -0400, Saaa <empty@needmail.com> wrote:
>
> "Steven Schveighoffer" <schveiguy@yahoo.com> wrote in message
> news:op.uzxs4wyreav7ka@localhost.localdomain...
>> On Sun, 06 Sep 2009 18:54:47 -0400, Saaa <empty@needmail.com> wrote:
>>
>>> 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 :)
>>
>> A delegate is a struct with a data pointer and a function pointer. You
>> can access the individual parts via .ptr and .func (I believe). You can
>> even change them via those properties.
>>
>> does that help?
>>
>> -Steve
>
> I did read that part.
> The problem lies more in that I'd like to point to something which is not
> there yet.
> In the code 'c.method()' is not there yet, as c is null.
> Maybe I should create a dummy object for c to point to in stead of null ?
> That way I point the delegate to the dummy method and ignore it as long as
> it is pointing
> to the dummy method :)
> The only drawback to this is that all objects I want to point the delegate
> to,
> need to somehow be convertable to the dummy type (interface/abstract class),
> meaning it will be less flexible.
Hm... I'm still confused. Why not just set the delegate to null? Why do you need to have the delegate set to something?
There are ways to do it, without having a class instance, but it is messy.
-Steve
|
September 08, 2009 Re: delegate !is null | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | >> The problem lies more in that I'd like to point to something which is not >> there yet. >> In the code 'c.method()' is not there yet, as c is null. >> Maybe I should create a dummy object for c to point to in stead of null ? >> That way I point the delegate to the dummy method and ignore it as long >> as >> it is pointing >> to the dummy method :) >> The only drawback to this is that all objects I want to point the >> delegate >> to, >> need to somehow be convertable to the dummy type (interface/abstract >> class), >> meaning it will be less flexible. > > Hm... I'm still confused. Why not just set the delegate to null? Why do you need to have the delegate set to something? It is for the gui. I give it a list of things to display. And some of these things don't yet exist or can be deleted at any time. I'd like it to display the last valid value. > > There are ways to do it, without having a class instance, but it is messy. how messy? :D Kind of related: If you delete an object and later create a new object, what are the chances they are located on the same place (deleted.ptr is new.ptr) ? Does the garbage collector try to reuse locations or is it the opposite (or random) ? |
September 08, 2009 Re: delegate !is null | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa Attachments: | Saaa wrote: >>> The problem lies more in that I'd like to point to something which is not >>> there yet. >>> In the code 'c.method()' is not there yet, as c is null. >>> Maybe I should create a dummy object for c to point to in stead of null ? >>> That way I point the delegate to the dummy method and ignore it as long >>> as >>> it is pointing >>> to the dummy method :) >>> The only drawback to this is that all objects I want to point the >>> delegate >>> to, >>> need to somehow be convertable to the dummy type (interface/abstract >>> class), >>> meaning it will be less flexible. >> Hm... I'm still confused. Why not just set the delegate to null? Why do you need to have the delegate set to something? > It is for the gui. I give it a list of things to display. > And some of these things don't yet exist or can be deleted at any time. > I'd like it to display the last valid value. > >> There are ways to do it, without having a class instance, but it is messy. > how messy? :D The only way I've found so far to do static binding like you are talking about is using string mixins. My port of Atl's window classes uses a MFC like message map: mixin messageMap!( msgRangeHdlr!(WM_MOUSEFIRST, WM_MOUSELAST, "wmMouseEvents"), msgHdlr!(WM_PAINT, "wmPaint"), msgHdlr!(WM_SIZE, "wmSize"), msgHdlr!(WM_KEYUP, "wmKeyUp"), msgHdlr!(WM_CHAR, "wmChar"), msgHdlr!(WM_SHOWWINDOW, "wmShow"), msgHdlr!(WM_CLOSE, "wmClose"), msgHdlr!(WM_CREATE, "wmCreate") ); Having to put the classes method in a string is a bit ugly, but without c++ style pointer to members I don't see any other way of doing it. If you want to have a look in more detail: http://www.sstk.co.uk/atlWinD.php > Kind of related: > If you delete an object and later create a new object, what are the chances > they are located on the > same place (deleted.ptr is new.ptr) ? > Does the garbage collector try to reuse locations or is it the opposite (or > random) ? Dunno, but the full source code is in dmd zip @ src\druntime\src\gc\basic\gcx.d if you are really that interested. - -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk |
September 08, 2009 Re: delegate !is null | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | On Tue, 08 Sep 2009 12:40:13 -0400, Saaa <empty@needmail.com> wrote: >> Hm... I'm still confused. Why not just set the delegate to null? Why do >> you need to have the delegate set to something? > It is for the gui. I give it a list of things to display. > And some of these things don't yet exist or can be deleted at any time. > I'd like it to display the last valid value. That still doesn't explain why you need to have delegates set to something valid, yet have a null pointer value. Either a) you are trying to subvert the type system, or b) you can simply set the whole delegate (function and pointer) once you get the class instance. It's hard for me to say what is the best way to do it, but generally, things like this are difficult, and rightfully so. Having a delegate without a valid pointer makes little sense. > >> >> There are ways to do it, without having a class instance, but it is messy. > how messy? :D class C { int val = 0; int method() {return val;} } int delegate() dg; dg.func = &C.method; // note the upper-case C // dg is now a "instanceless" delegate to C.method. dg.ptr = new C; Now you can call dg() and it will call the C.method function on the newly created C object. Note that this method of manipulating methods does NOT obey inheritance. For example: class B : C { int method() {return 555;} } dg.ptr = new B; dg(); // will return 0, since it calls C's version of method. I also don't know how well it will work on interfaces. > Kind of related: > If you delete an object and later create a new object, what are the chances > they are located on the > same place (deleted.ptr is new.ptr) ? > Does the garbage collector try to reuse locations or is it the opposite (or > random) ? The chances are non-zero :) I wouldn't depend on this behavior either way if I were you. -Steve |
Copyright © 1999-2021 by the D Language Foundation