comparing pointers passed to and returned from funcs

spir denis.spir at gmail.com
Tue Mar 1 10:47:09 PST 2011


Hello,

It seems to be the kind of stupid issue that will make you laugh about me. But 
I cannot grasp and want to move forward anyway; so, let us be bold and take the 
risk ;-)

I'm modeling a little dynamic language. Elements (values, objects) are pointers 
to structs (actually tagged unions) allocated on the heap. I have a problem in 
passing and returning those pointers to and from primitive procedures.

Precisely, unlike in D, Logical (boolean) operations only accept Logical 
elements true/false (called TRUE/FALSE on implementation side for obvious reason):
     enum TRUE = new DElement(true);
     enum FALSE = new DElement(false);

  So, I thought I could write those operations by comparing pointers directly, eg:
     Element not (Element operand) {
         // checks type
         operand.checkLogical()
         return (operand == TRUE) ? FALSE : TRUE;
     }
     ...
     assert ( not(TRUE) == FALSE );

This fails! It even fails "doubly"...
When I call not(TRUE), TRUE (the pointer) inside the func is not equal to the 
global constant. Thus, not always returns FALSE. And in fact this is also 
wrong, because I have the same problem on the return value as well: the FALSE 
returned is not equal to the global FALSE.

But the pointed structs are ok. Each one holds a D boolean of the correct value 
(a member called 'logical'). Thus, the following succeeds:

     Element not (Element operand) {
         // checks type & returns the 'logical' member
         auto log = operand.checkLogical();
         return (log) ? FALSE : TRUE;
     }
     ...
     assert ( not(TRUE).logical == false );

Here, I operate on the structs instead of on the pointers, both to perform the 
operation and in the assert. What I understand is: all happens like if D would 
copy the pointed structs on parameter passing and on return. I thought D would 
only copy the pointers (in both directions), which would let me compare said 
pointers directly.
What do I miss?

It is not a serious problem since the workaround is easy and not very costly. 
But I wish to understand why I cannot operate on constant 'identity'. As said 
above, this must a trivial issue I simply cannot guess...

Thank you,
Denis
-- 
_________________
vita es estrany
spir.wikidot.com



More information about the Digitalmars-d-learn mailing list