Assigning to array of structs with custom constructor
Ali Çehreli
acehreli at yahoo.com
Mon Apr 25 15:23:12 UTC 2022
On 4/25/22 07:36, cc wrote:
> ```d
> struct Foo {
> string s;
> this(string s) { this.s = s; }
> }
> Foo foo = "a";
I don't understand why that syntax exists. I always write it like this:
auto foo = Foo("a");
> Foo[] foos = ["a"]; // Error: cannot implicitly convert expression
> `["a"]` of type `string[]` to `Foo[]`
This is a way:
Foo[] foos = [Foo("a")];
Or:
import std.stdio;
import std.algorithm;
import std.range;
import std.conv;
auto arr = iota(10).map!(i => Foo(i.text)).array;
writeln(arr);
Ali
More information about the Digitalmars-d-learn
mailing list