View mode: basic / threaded / horizontal-split · Log in · Help
August 15, 2006
null assignment to bit variable
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
August 15, 2006
Re: null assignment to bit variable
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
August 15, 2006
Re: null assignment to bit variable
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 = 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


Perhaps bool/bit isn't even what you want.

enum Foo: byte
{
   None,
   True,
   False,
}

Foo f = Foo.None;

and this requires no heap allocation or extra storage.
August 15, 2006
Re: null assignment to bit variable
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
August 15, 2006
Re: null assignment to bit variable
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
August 15, 2006
Re: null assignment to bit variable
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
August 15, 2006
Re: null assignment to bit variable
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
August 15, 2006
Re: null assignment to bit variable
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
August 16, 2006
Re: null assignment to bit variable
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
Top | Discussion index | About this forum | D home