September 21, 2013 Re: Is there a way to see if .init has been set by the user? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Saturday, 21 September 2013 at 11:35:11 UTC, bearophile wrote:
>
> init is part of a type and you can't change it.
>
> Bye,
> bearophile
Well, D wouldn't be D, if it did not allow something like this for aggregate types:
import core.stdc.string, std.stdio;
pure hack(T)(T value) if (is(T == class))
{
byte[] init_mem = new byte[T.classinfo.init.length];
memcpy(init_mem.ptr, cast(byte*)value, T.classinfo.init.length);
T.classinfo.init = init_mem;
}
class A
{
int a;
}
pure foo(int val)
{
A a = new A;
a.a++;
hack(a);
return a.a + val;
}
void main()
{
writeln(0.foo, 0.foo, 0.foo); // prints 1,2,3
}
And taking into accout some additional runtime magic, assertion below may fail:
class A { int i = 5; }
struct S { A a;}
S s;
assert(s.a is null);
| |||
September 21, 2013 Re: Is there a way to see if .init has been set by the user? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Maxim Fomin | On Saturday, 21 September 2013 at 15:43:11 UTC, Maxim Fomin wrote:
> 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?
No, at compile-time. It's trivial to see if init is changed, but I want to see if it's set even if it's to 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 16:09:35 UTC, simendsjo wrote:
> On Saturday, 21 September 2013 at 15:43:11 UTC, Maxim Fomin wrote:
>> 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?
>
> No, at compile-time. It's trivial to see if init is changed, but I want to see if it's set even if it's to T.init.
I think there is a misunderstanding in your "a.init" and "b.init": This is just calling a static property on an instance. It's no different from T.init.
It *won't* tell you what a/b was initially initialized with.
If I understand correctly, you want:
int b = 3;
static assert(__traits(initialValue, b) == 3);
?
I don't think we have that. I'm not even sure we *can* have that.
| |||
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 16:09:35 UTC, simendsjo wrote:
> On Saturday, 21 September 2013 at 15:43:11 UTC, Maxim Fomin wrote:
>> 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?
>
> No, at compile-time. It's trivial to see if init is changed, but I want to see if it's set even if it's to T.init.
I see. No, I don't think this feature is suitable for declarative approach abuse ;)
Even if you manage to hack something, it is unlikely to be reliable, I'd recommend to stick to UDA's until compiler exposes that info.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply