www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - val.init

reply Nick Sabalausky <SeeWebsiteToContactMe semitwist.com> writes:
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
}
Oct 01 2013
next sibling parent Jacob Carlborg <doob me.com> writes:
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
Oct 01 2013
prev sibling next sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
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".
Oct 02 2013
prev sibling next sibling parent reply Sean Kelly <sean invisibleduck.org> writes:
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?:
=20
 import std.stdio;
 void main()
 {
 	int a =3D 5;
 	writeln(a.init); // Outputs 0, not 5
 }
I think it used to work roughly this way but was changed=85 um=85 maybe = 2 years ago?
Oct 02 2013
parent Jacob Carlborg <doob me.com> writes:
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
Oct 02 2013
prev sibling parent reply "Rene Zwanenburg" <renezwanenburg gmail.com> writes:
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
Oct 03 2013
parent Nick Sabalausky <SeeWebsiteToContactMe semitwist.com> writes:
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.
Oct 06 2013