Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
November 25, 2017 Is variable void? | ||||
---|---|---|---|---|
| ||||
Is there any way of determining whether a variable has been initialized or not? For example, if something is declared like this: int x = void; can I check if it's void before I use it, say, in a function it's been passed to? |
November 25, 2017 Re: Is variable void? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Chapman | On 25/11/2017 3:34 PM, John Chapman wrote:
> Is there any way of determining whether a variable has been initialized or not? For example, if something is declared like this:
>
> int x = void;
>
> can I check if it's void before I use it, say, in a function it's been passed to?
`` = void;`` isn't null.
Don't treat it as such. It is a low level detail where you _will_ initialize it, just in a smarter way.
There is no conditions tied directly to it.
|
November 25, 2017 Re: Is variable void? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Chapman | On Saturday, 25 November 2017 at 15:34:21 UTC, John Chapman wrote:
> Is there any way of determining whether a variable has been initialized or not? For example, if something is declared like this:
nope. It'd be indistinguishable from the user just happening to initialize it to some random value.
|
November 25, 2017 Re: Is variable void? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Saturday, 25 November 2017 at 15:38:15 UTC, Adam D. Ruppe wrote:
> nope. It'd be indistinguishable from the user just happening to initialize it to some random value.
Thanks. I'll got with .init instead.
|
November 26, 2017 Re: Is variable void? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Chapman | On Saturday, 25 November 2017 at 15:34:21 UTC, John Chapman wrote:
> Is there any way of determining whether a variable has been initialized or not? For example, if something is declared like this:
>
> int x = void;
>
> can I check if it's void before I use it, say, in a function it's been passed to?
You can use Nullable!int
|
November 27, 2017 Re: Is variable void? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Chapman | On Saturday, 25 November 2017 at 15:34:21 UTC, John Chapman wrote: > Is there any way of determining whether a variable has been initialized or not? For example, if something is declared like this: > > int x = void; > > can I check if it's void before I use it, say, in a function it's been passed to? // ---------------------------------- module test; import std.stdio; import std.typecons; // see: https://dlang.org/phobos/std_typecons.html#Nullable void main() { Nullable!int x; // requires: import std.typecons assert(x.isNull); writeln("x is ", x); x = 1; assert(!x.isNull); writeln("x is ", x); x.nullify(); // Forces x back to a null state. assert(x.isNull); writeln("x is ", x); } // ---------------------------------- |
November 28, 2017 Re: Is variable void? | ||||
---|---|---|---|---|
| ||||
Posted in reply to codephantom | On Monday, 27 November 2017 at 02:12:40 UTC, codephantom wrote:
> On Saturday, 25 November 2017 at 15:34:21 UTC, John Chapman wrote:
>> Is there any way of determining whether a variable has been initialized or not? For example, if something is declared like this:
>>
>> int x = void;
>>
>> can I check if it's void before I use it, say, in a function it's been passed to?
>
> // ----------------------------------
>
> module test;
>
> import std.stdio;
> import std.typecons; // see: https://dlang.org/phobos/std_typecons.html#Nullable
>
> void main()
> {
> Nullable!int x; // requires: import std.typecons
> assert(x.isNull);
> writeln("x is ", x);
>
> x = 1;
> assert(!x.isNull);
> writeln("x is ", x);
>
> x.nullify(); // Forces x back to a null state.
> assert(x.isNull);
> writeln("x is ", x);
>
> }
> // ----------------------------------
null != void
|
November 28, 2017 Re: Is variable void? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bauss | On Tuesday, 28 November 2017 at 05:10:39 UTC, bauss wrote:
> null != void
"initialized or not?" != void
|
November 28, 2017 Re: Is variable void? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bauss | On Tuesday, 28 November 2017 at 05:10:39 UTC, bauss wrote: > null != void also...void is a completely useless concept for initialisation. what can you determine about the nothingness of void? ... nothing. writeln(typeof(void).stringof); // ?? what do I know now? nothing. vs Nullable!int x; writeln(typeof(x).stringof); // Nullable!int .. now I know something. |
Copyright © 1999-2021 by the D Language Foundation