Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 21, 2013 Is there a way to see if .init has been set by the user? | ||||
---|---|---|---|---|
| ||||
I want to know if a variable has changed .init, but I don't know if it's possible if the .init value is the same. Does anyone have a solution for this? int a; int b = 0; pragma(msg, a.init); // 0 pragma(msg, b.init); // 0 // how can I see that b has "= 0"? |
September 21, 2013 Re: Is there a way to see if .init has been set by the user? | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | simendsjo:
> I want to know if a variable has changed .init, but I don't know if it's possible if the .init value is the same. Does anyone have a solution for this?
>
> int a;
> int b = 0;
> pragma(msg, a.init); // 0
> pragma(msg, b.init); // 0
> // how can I see that b has "= 0"?
init is part of a type and you can't change it. a and by are both ints, and in both cases int.init is 0. I think there is no simple way to know if a variable has being default-initialized or not. Why do you want to know it?
Bye,
bearophile
|
September 21, 2013 Re: Is there a way to see if .init has been set by the user? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 9/21/13, bearophile <bearophileHUGS@lycos.com> wrote:
> I think there is no simple
> way to know if a variable has being default-initialized or not.
> Why do you want to know it?
There is however a way to extract it if it's a part of an aggregate:
-----
struct S
{
int x = 1;
}
void main()
{
static assert(S.x.init == 0);
static assert(S.init.x == 1);
}
-----
|
September 21, 2013 Re: Is there a way to see if .init has been set by the user? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On Saturday, 21 September 2013 at 12:50:20 UTC, Andrej Mitrovic wrote:
> On 9/21/13, bearophile <bearophileHUGS@lycos.com> wrote:
>> I think there is no simple
>> way to know if a variable has being default-initialized or not.
>> Why do you want to know it?
>
> There is however a way to extract it if it's a part of an aggregate:
>
> -----
> struct S
> {
> int x = 1;
> }
>
> void main()
> {
> static assert(S.x.init == 0);
> static assert(S.init.x == 1);
> }
> -----
I'm though of using if for aggregates, yes. But this doesn't handle the case where you set the same value is T.init.
|
September 21, 2013 Re: Is there a way to see if .init has been set by the user? | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | On Saturday, 21 September 2013 at 13:30:19 UTC, simendsjo wrote:
> I'm though of using if for aggregates, yes. But this doesn't handle the case where you set the same value is T.init.
I don't think those two case are any different from the type system point of view, manual or default.
|
September 21, 2013 Re: Is there a way to see if .init has been set by the user? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | On Saturday, 21 September 2013 at 13:38:44 UTC, Dicebot wrote:
> On Saturday, 21 September 2013 at 13:30:19 UTC, simendsjo wrote:
>> I'm though of using if for aggregates, yes. But this doesn't handle the case where you set the same value is T.init.
>
> I don't think those two case are any different from the type system point of view, manual or default.
Need some __traits(hasUserSuppliedInit, T) then :)
|
September 21, 2013 Re: Is there a way to see if .init has been set by the user? | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | On Saturday, 21 September 2013 at 13:48:00 UTC, simendsjo wrote:
> On Saturday, 21 September 2013 at 13:38:44 UTC, Dicebot wrote:
>> On Saturday, 21 September 2013 at 13:30:19 UTC, simendsjo wrote:
>>> I'm though of using if for aggregates, yes. But this doesn't handle the case where you set the same value is T.init.
>>
>> I don't think those two case are any different from the type system point of view, manual or default.
>
> Need some __traits(hasUserSuppliedInit, T) then :)
I am not sure those differ even at compiler level (other than at syntax stage) - why would you need it?
|
September 21, 2013 Re: Is there a way to see if .init has been set by the user? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | On Saturday, 21 September 2013 at 13:55:00 UTC, Dicebot wrote:
> On Saturday, 21 September 2013 at 13:48:00 UTC, simendsjo wrote:
>> On Saturday, 21 September 2013 at 13:38:44 UTC, Dicebot wrote:
>>> On Saturday, 21 September 2013 at 13:30:19 UTC, simendsjo wrote:
>>>> I'm though of using if for aggregates, yes. But this doesn't handle the case where you set the same value is T.init.
>>>
>>> I don't think those two case are any different from the type system point of view, manual or default.
>>
>> Need some __traits(hasUserSuppliedInit, T) then :)
>
> I am not sure those differ even at compiler level (other than at syntax stage) - why would you need it?
I was thinking of "query by example". If a field has been set, it should be used in a where clause. But if I cannot see the difference between int.init and 0 for instance, it is impossible to get it right.
It's possible to solve this using UDAs of course.
|
September 21, 2013 Re: Is there a way to see if .init has been set by the user? | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | On Saturday, 21 September 2013 at 14:11:44 UTC, simendsjo wrote:
> On Saturday, 21 September 2013 at 13:55:00 UTC, Dicebot wrote:
>> On Saturday, 21 September 2013 at 13:48:00 UTC, simendsjo wrote:
>>> On Saturday, 21 September 2013 at 13:38:44 UTC, Dicebot wrote:
>>>> On Saturday, 21 September 2013 at 13:30:19 UTC, simendsjo wrote:
>>>>> I'm though of using if for aggregates, yes. But this doesn't handle the case where you set the same value is T.init.
>>>>
>>>> I don't think those two case are any different from the type system point of view, manual or default.
>>>
>>> Need some __traits(hasUserSuppliedInit, T) then :)
>>
>> I am not sure those differ even at compiler level (other than at syntax stage) - why would you need it?
>
> I was thinking of "query by example". If a field has been set, it should be used in a where clause. But if I cannot see the difference between int.init and 0 for instance, it is impossible to get it right.
> It's possible to solve this using UDAs of course.
0 and int.init is the same. ;)
|
September 21, 2013 Re: Is there a way to see if .init has been set by the user? | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | On Saturday, 21 September 2013 at 11:13:57 UTC, simendsjo wrote:
> I want to know if a variable has changed .init, but I don't know if it's possible if the .init value is the same. Does anyone have a solution for this?
>
> int a;
> int b = 0;
> pragma(msg, a.init); // 0
> pragma(msg, b.init); // 0
> // how can I see that b has "= 0"?
Strictly speaking this is an ill-posed question. Are talking about testing at runtime whether some variable was changed from init to some non-default value?
|
Copyright © 1999-2021 by the D Language Foundation