Thread overview | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 23, 2012 struct init property | ||||
---|---|---|---|---|
| ||||
struct has no default constructor and instances are initialized with the init property. Can I declare or override the init property for a custom defined struct? |
August 23, 2012 Re: struct init property | ||||
---|---|---|---|---|
| ||||
Posted in reply to nocide | On 08/23/2012 10:15 AM, nocide wrote:
> struct has no default constructor and instances are initialized with the
> init property.
> Can I declare or override the init property for a custom defined struct?
>
You can define the initial values of each member:
struct S
{
int i = 42;
double d = 1.5;
}
void main()
{
assert(S.init == S(42, 1.5));
}
That changes the .init value. You can also define a static opCall() to mimic the default constructor. Note that this method does not affect the .init value:
struct S
{
int i;
double d;
static S opCall()
{
S s;
s.i = 42;
s.d = 1.5;
return s;
}
}
void main()
{
assert(S().i == 42);
assert(S().d == 1.5);
assert(S.init.i == int.init);
import std.math;
assert(isnan(S.init.d));
}
Note that the static opCall() disables calling automatic destructor (i.e. the S(1, 2) does not work anymore). I am not sure whether that is one of the many struct-related issues at the moment.
Ali
|
August 23, 2012 Re: struct init property | ||||
---|---|---|---|---|
| ||||
Posted in reply to nocide | Am 23.08.2012 19:15, schrieb nocide:
> struct has no default constructor and instances are initialized with the
> init property.
> Can I declare or override the init property for a custom defined struct?
>
I've just realized, that I just have to declare default initializer for the members. This also works with unions!
struct Foo {
int a;
union {
int b = 7;
float c;
}
}
|
August 23, 2012 Re: struct init property | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | [snip] Ah thanks! You just were a little bit faster :) |
August 23, 2012 Re: struct init property | ||||
---|---|---|---|---|
| ||||
Posted in reply to nocide | On Thursday, August 23, 2012 19:15:16 nocide wrote:
> struct has no default constructor and instances are initialized with the
> init property.
> Can I declare or override the init property for a custom defined struct?
The init property is defined by what you directly initialize the struct's member variables to.
struct S
{
int i = 7;
string s = "hello";
}
S.init has a value of 7 for i and "hello" for s.
Unfortunately, I don't believe that declaring an init function or property is currently disallowed, but if it affects S.init, it's a bug.
- Jonathan M Davis
|
August 23, 2012 Re: struct init property | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | It would be great if you could declare your own .init as you can atm with "new" (http://dlang.org/class.html#allocators). For example: class Foo { public: init() { this = new typeof(this)(); } } So you could avoid a null declaration by default. :) |
August 23, 2012 Re: struct init property | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | On 23-Aug-12 22:44, Namespace wrote: > It would be great if you could declare your own .init as you can atm > with "new" (http://dlang.org/class.html#allocators). > Read on : Note: Class allocators are deprecated in D2. > For example: > > class Foo { > public: > init() { > this = new typeof(this)(); > } > } > > So you could avoid a null declaration by default. :) -- Olshansky Dmitry |
August 23, 2012 Re: struct init property | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dmitry Olshansky | But to write your own init property would still be nice, or not? |
August 23, 2012 Re: struct init property | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | On 23-Aug-12 23:33, Namespace wrote: > But to write your own init property would still be nice, or not? Either way it has to be CTFEable. Can't you just do: struct A{ X field = generateX(); } and thus set .init to whatever you want. Otherwise it will inevitably rise question of the point of "= x;" syntax if initializes are overridden by custom .init. -- Olshansky Dmitry |
August 23, 2012 Re: struct init property | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | On Thursday, August 23, 2012 21:33:57 Namespace wrote:
> But to write your own init property would still be nice, or not?
How would it be different from defining a default constructor? Structs specifically _don't_ have default constructors, because init must be known at compile time, and all kinds of restrictions would have to be placed on a default constructor (enough to make it pointless) to ensure that it would work as an init value that there's really no point to it. The kind of stuff that you'd want to do in a default constructor but can't do by directly initializing the member variables is precisely the kind of stuff that you _can't_ do with a struct, because init must be known at compile time, must always generate the same result, couldn't throw exceptions, etc.
So, sure, at times it would be great to have a default constructor for structs, but other design decisions in the language (particularly with regards to init) simply make that infeasible. It's one of those "forced faults" in language design that Andrei likes to talk about.
So, what we get is an init value defined by how the member variables are directly initialized and the ability to define a static opCall to get you the equivalent of the default constructor in cases where S() is used rather than S.init. It's not perfect, but there's not much that we can do about it.
- Jonathan M Davis
|
Copyright © 1999-2021 by the D Language Foundation