Member field of type nested struct must be initialized in constructor: Why?

Inkrementator anon at anon.org
Sun Oct 22 21:02:32 UTC 2023


Consider this almost minimal example:
```
import std.algorithm;
import std.range;
import std.stdio;

struct S(Nested){
	Nested member; // = I.init; // Uncommenting this wouldn't help
	int g;
	this(Nested member){
		this.member = member;
	}
	
	this(int g){
		this.g = g;
		version(fix)
			member = Nested.init;
	}
	
}

// IFTI to handle type of map
auto makeS(Nested)(Nested member) => S!(Nested)(member);

import std.sumtype;
// This just works and needs no fix
auto makeSum(I)(I i) => SumType!(string, I)(i);

void main(){
	auto input = iota(5).map!(b => b + 1).map!(b => b - 1);

	auto s = makeS(input);
	auto s2 = makeSum(input);
	writeln(s);
	writeln(s2);
}
```

Running the code with `rdmd -version=fix app.d` works, but 
running `rdmd -version=fix app.d` produces:
`Error: field `member` must be initialized in constructor, 
because it is nested struct`
Why?
I didn't find anything about this in [the 
spec.](https://dlang.org/spec/struct.html#nested)


More information about the Digitalmars-d-learn mailing list