Concerns about struct initialization
Dru
Dru at notreal.com
Sat Dec 22 08:53:20 UTC 2018
Hello,
I have two concerns about struct initialization
It would be nice to hear your opinion
1)
would like to override default copy initialization
code example:
import std.stdio;
void main(){
struct A{
this(int n) {
writeln("construct int");
}
this(ref const A other) {
writeln("construct ref A");
}
ref A opAssign(ref const A other) {
writeln("opAssign");
return this;
}
}
A a1 = A(1);
A a2 = A(a1);
A a3 = 1;
A a4;
a4 = a1;
A a5 = a1; //does not call constructor or opAssign
}
2)
I was reading this:
https://dlang.org/spec/struct.html#dynamic_struct_init
It's pretty weird that a static opCall works as a constructor
Problems are:
- it doesn't work when ANY constructor is defined
- it blocks calling to default constructor
code example:
import std.stdio;
struct A{
static A opCall(int n) {
writeln("opCall");
A a;
return a;
}
//adding a constructor will error
//this(string str) {}
}
void main(){
A a1 = 1; //calls opCall
//calling default constructor will error
//A a2 = A();
}
More information about the Digitalmars-d
mailing list