Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
October 02, 2013 val.init | ||||
---|---|---|---|---|
| ||||
I thought variable.init was different from T.init and gave the value of the explicit initializer if one was used. Was I mistaken?: import std.stdio; void main() { int a = 5; writeln(a.init); // Outputs 0, not 5 } |
October 02, 2013 Re: val.init | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | On 2013-10-02 04:10, Nick Sabalausky wrote: > I thought variable.init was different from T.init and gave the value of > the explicit initializer if one was used. Was I mistaken?: Yes. -- /Jacob Carlborg |
October 02, 2013 Re: val.init | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | On Wednesday, 2 October 2013 at 02:10:35 UTC, Nick Sabalausky wrote:
> I thought variable.init was different from T.init and gave the value of
> the explicit initializer if one was used. Was I mistaken?:
>
> import std.stdio;
> void main()
> {
> int a = 5;
> writeln(a.init); // Outputs 0, not 5
> }
AFAIK "variable.init" just triggers the "static call through instance" mechanic:
The *variable* a doesn't actually have .init, so the call resolves to "int.init".
|
October 02, 2013 Re: val.init | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | On Oct 1, 2013, at 7:10 PM, Nick Sabalausky <SeeWebsiteToContactMe@semitwist.com> wrote: > I thought variable.init was different from T.init and gave the value of the explicit initializer if one was used. Was I mistaken?: > > import std.stdio; > void main() > { > int a = 5; > writeln(a.init); // Outputs 0, not 5 > } I think it used to work roughly this way but was changed… um… maybe 2 years ago? |
October 03, 2013 Re: val.init | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | On 2013-10-02 19:54, Sean Kelly wrote: > I think it used to work roughly this way but was changed… um… maybe 2 years ago? It has worked like this for as long as I can remember. I've been using D for 6-7 years. -- /Jacob Carlborg |
October 03, 2013 Re: val.init | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | On Wednesday, 2 October 2013 at 02:10:35 UTC, Nick Sabalausky wrote: > I thought variable.init was different from T.init and gave the value of > the explicit initializer if one was used. Was I mistaken?: > > import std.stdio; > void main() > { > int a = 5; > writeln(a.init); // Outputs 0, not 5 > } Not exactly. The spec does mention something similar regarding a typedef [1]. Since typedef is deprecated I've never used it, but IIRC it's effect is similar to defining a struct with a single member. From this point of view it makes sense the init of a typedef'ed primitive type is not necessarily equal to that primitive type's init, since struct A { int i = 5; } void main() { writeln(A.init.i); } prints 5. [1] http://dlang.org/property#init |
October 06, 2013 Re: val.init | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rene Zwanenburg | On Thu, 03 Oct 2013 11:59:05 +0200
"Rene Zwanenburg" <renezwanenburg@gmail.com> wrote:
>
> struct A { int i = 5; }
> void main() { writeln(A.init.i); }
>
> prints 5.
>
Ahh, ok, I think that's what confused me.
|
Copyright © 1999-2021 by the D Language Foundation