digitalmars.D.learn - Compare struct against null
- Heinz (14/14) Apr 27 2008 Hi,
- downs (5/24) Apr 28 2008 Structs are value types. Read the spec. Then use a pointer :)
- Heinz (4/32) Apr 28 2008 Thanks for your reply. Can i use then?:
- Simen Kjaeraas (17/28) Apr 28 2008 No. &h will always return a non-null pointer (h exists, in other words)....
- downs (6/39) Apr 29 2008 No.
Hi, I have a simple struct and i want to check if a variable has been asigned: struct hello { } ... hello h; ... if(!h) { do stuff; } When compiling i get erros about opCall not implemented and many others. What can i do? Thanks in advance.
Apr 27 2008
Heinz wrote:Hi, I have a simple struct and i want to check if a variable has been asigned: struct hello { } ... hello h; ... if(!h) { do stuff; } When compiling i get erros about opCall not implemented and many others. What can i do? Thanks in advance.Structs are value types. Read the spec. Then use a pointer :) hello* h; if (!h) h = new hello; --downs
Apr 28 2008
downs Wrote:Heinz wrote:Thanks for your reply. Can i use then?: hello h; if(!&h)Hi, I have a simple struct and i want to check if a variable has been asigned: struct hello { } ... hello h; ... if(!h) { do stuff; } When compiling i get erros about opCall not implemented and many others. What can i do? Thanks in advance.Structs are value types. Read the spec. Then use a pointer :) hello* h; if (!h) h = new hello; --downs
Apr 28 2008
On Tue, 29 Apr 2008 06:50:18 +0200, Heinz <malagana15 yahoo.es> wrote:downs Wrote:No. &h will always return a non-null pointer (h exists, in other words).= You might use some convoluted workaround to make hello aware of whether = or not it has been assigned something. Easy example: struct hello { bool assigned; // other fields; } hello h; h =3D hello(true, ...); if (h.assigned) DoSomethingOrOther(); This may or may not be enough for you needs. -- SimenStructs are value types. Read the spec. Then use a pointer :) hello* h; if (!h) h =3D new hello; --downsThanks for your reply. Can i use then?: hello h; if(!&h)
Apr 28 2008
Heinz wrote:downs Wrote:No. Structs are *value types*. That means, unless _explicitly newed_ (or part of a struct that's explicitly newed), they _always_ live on the stack. Ergo, if &h is null, your complete callstack is shot :) --downsHeinz wrote:Thanks for your reply. Can i use then?: hello h; if(!&h)Hi, I have a simple struct and i want to check if a variable has been asigned: struct hello { } ... hello h; ... if(!h) { do stuff; } When compiling i get erros about opCall not implemented and many others. What can i do? Thanks in advance.Structs are value types. Read the spec. Then use a pointer :) hello* h; if (!h) h = new hello; --downs
Apr 29 2008