www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - null assignment to bit variable

reply Peter Thomassen <info peter-thomassen.de> writes:
Hi,

the following doesn't work:

bit test; 
test = null; 
 
I understand that this is not possible, but coming from the PHP world, I'm
looking for a way to define that "test" hasn't got a value yet, not even
true or false.

Is that possible? Is there a workaround?

Thanks!
Peter
Aug 14 2006
next sibling parent reply Sean Kelly <sean f4.ca> writes:
Peter Thomassen wrote:
 Hi,
 
 the following doesn't work:
 
 bit test; 
 test = null; 
  
 I understand that this is not possible, but coming from the PHP world, I'm
 looking for a way to define that "test" hasn't got a value yet, not even
 true or false.
 
 Is that possible? Is there a workaround?
bool* test = null; test = new bool; (note that bit has been replaced by bool in recent versions of D) Sean
Aug 14 2006
parent reply Peter Thomassen <info peter-thomassen.de> writes:
Sean Kelly schrieb am Dienstag, 15. August 2006 03:34:
      bool* test = null;
 
      test = new bool;
This looks nice ... but how do I read / write the value of test? After assigning "new bool", reading it gives a memory address, and writing fails. Maybe I should've posted to digitalmars.D.learn :-)
 (note that bit has been replaced by bool in recent versions of D)
Thanks. Peter
Aug 15 2006
parent reply Sean Kelly <sean f4.ca> writes:
Peter Thomassen wrote:
 Sean Kelly schrieb am Dienstag, 15. August 2006 03:34:
      bool* test = null;

      test = new bool;
This looks nice ... but how do I read / write the value of test? After assigning "new bool", reading it gives a memory address, and writing fails. Maybe I should've posted to digitalmars.D.learn :-)
The other suggestion to use an enum is a better one. To use the bool above you have to dereference the pointer: if( test !is null && *test ) { // something that requires test to be set and true } Sean
Aug 15 2006
parent reply Peter Thomassen <info peter-thomassen.de> writes:
Sean Kelly schrieb am Dienstag, 15. August 2006 23:59:
      if( test !is null && *test )
      {
          // something that requires test to be set and true
      }
And how would I set it false? Peter
Aug 15 2006
parent reply Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
Peter Thomassen wrote:
 Sean Kelly schrieb am Dienstag, 15. August 2006 23:59:
 
     if( test !is null && *test )
     {
         // something that requires test to be set and true
     }
And how would I set it false? Peter
*test = false; -- Chris Nicholson-Sauls
Aug 15 2006
parent Peter Thomassen <info peter-thomassen.de> writes:
Chris Nicholson-Sauls schrieb am Mittwoch, 16. August 2006 02:01:
 And how would I set it false?
*test = false;
I'm that stupid -- thanks. Peter
Aug 16 2006
prev sibling next sibling parent "Chris Miller" <chris dprogramming.com> writes:
On Mon, 14 Aug 2006 21:22:54 -0400, Peter Thomassen  =

<info peter-thomassen.de> wrote:

 Hi,

 the following doesn't work:

 bit test;
 test =3D null;
 I understand that this is not possible, but coming from the PHP world,=
=
 I'm
 looking for a way to define that "test" hasn't got a value yet, not ev=
en
 true or false.

 Is that possible? Is there a workaround?

 Thanks!
 Peter
Perhaps bool/bit isn't even what you want. enum Foo: byte { None, True, False, } Foo f =3D Foo.None; and this requires no heap allocation or extra storage.
Aug 14 2006
prev sibling parent "Unknown W. Brackets" <unknown simplemachines.org> writes:
Just remember, D is not PHP :).

As it would happen, the way PHP/Zend does it is using a struct:

enum zval_type
{
	IS_NULL,
	IS_STRING,
	IS_INTEGER,
}

struct zval
{
	zval_type type = zval_type.IS_NULL;
	union
	{
		char[] str_val = null;
		int int_val = 0;
	}
}

I'm also primarily a PHP programmer, by the way ;).

-[Unknown]


 Hi,
 
 the following doesn't work:
 
 bit test; 
 test = null; 
  
 I understand that this is not possible, but coming from the PHP world, I'm
 looking for a way to define that "test" hasn't got a value yet, not even
 true or false.
 
 Is that possible? Is there a workaround?
 
 Thanks!
 Peter
Aug 14 2006