| Thread overview | |||||||
|---|---|---|---|---|---|---|---|
  | 
May 20, 2011 .init of field == type's initializer or field initializer? | ||||
|---|---|---|---|---|
  | ||||
From: http://d-programming-language.org/property.html .init Property: ".init produces a constant expression that is the default initializer. If applied to a type, it is the default initializer for that type. If applied to a variable or field, it is the default initializer for that variable or field. For example: " struct Foo { int a; int b = 7; } Foo.a.init // is 0 Foo.b.init // is 7 Foo.b.init is actually 0. Are the docs wrong, or is the compiler wrong? Let me know so I can fix the docs if necessary as I'm doing that now.  | ||||
May 20, 2011 Re: .init of field == type's initializer or field initializer? | ||||
|---|---|---|---|---|
  | ||||
Posted in reply to Andrej Mitrovic  | On 2011-05-19 20:19, Andrej Mitrovic wrote:
> From: http://d-programming-language.org/property.html
> .init Property:
> ".init produces a constant expression that is the default initializer. If
> applied to a type, it is the default initializer for that type. If applied
> to a variable or field, it is the default initializer for that variable or
> field. For example: "
> 
> struct Foo
> {
>     int a;
>     int b = 7;
> }
> 
> Foo.a.init	// is 0
> Foo.b.init	// is 7
> 
> Foo.b.init is actually 0. Are the docs wrong, or is the compiler wrong? Let me know so I can fix the docs if necessary as I'm doing that now.
import std.stdio;
import std.string;
struct Foo
{
    int a;
    int b = 7;
}
void main()
{
    assert(Foo.init.a == 0);
    assert(Foo.init.b == 7);
    assert(Foo.a.init == 0);
    assert(Foo.b.init == 0);
}
This runs and passes. It is correct. I believe that you are confusing Foo.init.b and Foo.b.init. The value of b for Foo.init is 7, but the init value for an int is always 0.
- Jonathan M Davis
 | |||
May 20, 2011 Re: .init of field == type's initializer or field initializer? | ||||
|---|---|---|---|---|
  | ||||
It was in the docs like that actually.  | ||||
May 20, 2011 Re: .init of field == type's initializer or field initializer? | ||||
|---|---|---|---|---|
  | ||||
On 2011-05-19 22:51, Andrej Mitrovic wrote:
> It was in the docs like that actually.
Well, then it's an error in the docs. Foo.init should have its member variables initialized to what they were directly initialized to, but that shouldn't affect the default initializers of the types of the member variables.
- Jonathan M Davis
 | ||||
May 21, 2011 Re: .init of field == type's initializer or field initializer? | ||||
|---|---|---|---|---|
  | ||||
Posted in reply to Andrej Mitrovic  | On 20/05/2011 04:19, Andrej Mitrovic wrote: <snip> > Foo.b.init is actually 0. Are the docs wrong, or is the compiler wrong? Let me know so > I can fix the docs if necessary as I'm doing that now. Known issue: http://d.puremagic.com/issues/show_bug.cgi?id=5715 Stewart.  | |||
Copyright © 1999-2021 by the D Language Foundation
 
Permalink
Reply