www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - docs/definition: !object

reply "Nick Sabalausky (Abscissa)" <SeeWebsiteToContactMe semitwist.com> writes:
I'm having trouble finding the documentation for what exactly the unary 
"not" operator does when applied to a class/interface object. Does this 
documentation exist somewhere?

I know at least part of it involves "is null", but I seem to remember 
hearing there was more to it than just that.
Mar 07 2018
parent reply ketmar <ketmar ketmar.no-ip.org> writes:
Nick Sabalausky (Abscissa) wrote:

 I'm having trouble finding the documentation for what exactly the unary 
 "not" operator does when applied to a class/interface object. Does this 
 documentation exist somewhere?

 I know at least part of it involves "is null", but I seem to remember 
 hearing there was more to it than just that.
https://dlang.org/spec/operatoroverloading.html#boolean_operators "Class references are converted to bool by checking to see if the class reference is null or not."
Mar 07 2018
parent reply "Nick Sabalausky (Abscissa)" <SeeWebsiteToContactMe semitwist.com> writes:
On 03/08/2018 12:05 AM, ketmar wrote:
 Nick Sabalausky (Abscissa) wrote:
 
 I'm having trouble finding the documentation for what exactly the 
 unary "not" operator does when applied to a class/interface object. 
 Does this documentation exist somewhere?

 I know at least part of it involves "is null", but I seem to remember 
 hearing there was more to it than just that.
https://dlang.org/spec/operatoroverloading.html#boolean_operators "Class references are converted to bool by checking to see if the class reference is null or not."
Ah, thanks. But are we CERTAIN that's all there is to it? I have a non-reduced situation right now where outputting the address of a class reveals a non-null address, and yet assert(!!theObjectInQuestion) is failing. (this is occurring during stack unwinding, if that makes a difference) (Or does &someObject return the address of the *reference* to the object rather than the address of the object?...You can see just how often I do OO in D ;) )
Mar 07 2018
next sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
Nick Sabalausky (Abscissa) wrote:

 (Or does &someObject return the address of the *reference* to the object 
 rather than the address of the object?...You can see just how often I do 
 OO in D ;) )
exactly. if you want to convert object to a pointer safely, do this: MyObject o; void* p = *cast(void**)&o; this magic construct guarantees that you won't hit `opCast()` in `MyObject` (yes, somebody *can* write weird `opCast` for `void*`! ;-). doing just `&o` gives you the address of a variable (on a stack, or in a tls), which is, obviously, never `null`. ;-)
Mar 07 2018
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 3/8/18 1:00 AM, Nick Sabalausky (Abscissa) wrote:
 On 03/08/2018 12:05 AM, ketmar wrote:
 Nick Sabalausky (Abscissa) wrote:

 I'm having trouble finding the documentation for what exactly the 
 unary "not" operator does when applied to a class/interface object. 
 Does this documentation exist somewhere?

 I know at least part of it involves "is null", but I seem to remember 
 hearing there was more to it than just that.
https://dlang.org/spec/operatoroverloading.html#boolean_operators "Class references are converted to bool by checking to see if the class reference is null or not."
Ah, thanks. But are we CERTAIN that's all there is to it? I have a non-reduced situation right now where outputting the address of a class reveals a non-null address, and yet assert(!!theObjectInQuestion) is failing. (this is occurring during stack unwinding, if that makes a difference)
One thing to keep in mind, assert(someObject) does more than just check if it's not null, it also runs the object's invariant to see if it's valid. I'm not sure about assert(!!someObject), probably this is just checking for non-null, but I'm not sure if the compiler folds this into assert(someObject) (which would be a bug, but may explain things). To be more pedantic, I'd recommend assert(someObject !is null) -Steve
Mar 08 2018
parent "Nick Sabalausky (Abscissa)" <SeeWebsiteToContactMe semitwist.com> writes:
On 03/08/2018 05:31 AM, Steven Schveighoffer wrote:
 On 3/8/18 1:00 AM, Nick Sabalausky (Abscissa) wrote:
 But are we CERTAIN that's all there is to it? I have a non-reduced 
 situation right now where outputting the address of a class reveals a 
 non-null address, and yet assert(!!theObjectInQuestion) is failing. 
 (this is occurring during stack unwinding, if that makes a difference)
Turns out it wasn't a class at all: As Jacob pointed out in my other thread, it *used* to be a class/interface in a previous lib version (vibe's TCPConnection) but got changed to a struct without my noticing. Hence the seemingly weird behaviours.
 
 One thing to keep in mind, assert(someObject) does more than just check 
 if it's not null, it also runs the object's invariant to see if it's valid.
 
Ahh, right! *That's* the part I knew I'd heard about and was trying to remember.
Mar 08 2018