digitalmars.D.learn - container vs standard array
- vino (23/23) Sep 18 2023 Hi All,
- JG (4/27) Sep 18 2023 Looks to me like when it receives a single range it expects the
- Nick Treleaven (11/17) Sep 19 2023 Yes, the first overload fails to match because U is inferred as
- Nick Treleaven (3/8) Sep 19 2023 https://github.com/dlang/phobos/pull/8818
- vino (3/12) Sep 19 2023 Thank you very much.
Hi All, I am trying to understand as to why the below code is throwing error Code ``` import std.stdio: writeln; import std.container.array; void main () { //auto a = Array!string("Aname"); // throws error auto b = Array!char("Bname"); // works auto c = Array!string("Aname", "Bname"); // works //writeln(a); writeln("Container Array :", b); writeln("Container Array :", c); writeln(); string[] d = ["Dname"]; // works string[] e = ["Dname", "Ename"]; // works writeln("Standard Array :", d); writeln("Standard Array :", e); } ``` From, Vino
Sep 18 2023
On Tuesday, 19 September 2023 at 00:34:01 UTC, vino wrote:Hi All, I am trying to understand as to why the below code is throwing error Code ``` import std.stdio: writeln; import std.container.array; void main () { //auto a = Array!string("Aname"); // throws error auto b = Array!char("Bname"); // works auto c = Array!string("Aname", "Bname"); // works //writeln(a); writeln("Container Array :", b); writeln("Container Array :", c); writeln(); string[] d = ["Dname"]; // works string[] e = ["Dname", "Ename"]; // works writeln("Standard Array :", d); writeln("Standard Array :", e); } ``` From, VinoLooks to me like when it receives a single range it expects the elements of that range to match the type. If I am correct in the first one if you replace the "Aname" by ["Aname"] it should work
Sep 18 2023
On Tuesday, 19 September 2023 at 06:35:01 UTC, JG wrote:On Tuesday, 19 September 2023 at 00:34:01 UTC, vino wrote:...//auto a = Array!string("Aname"); // throws error auto b = Array!char("Bname"); // works auto c = Array!string("Aname", "Bname"); // worksLooks to me like when it receives a single range it expects the elements of that range to match the type.Yes, the first overload fails to match because U is inferred as immutable(char) rather than string: this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) This is because a single array can be passed to a typesafe variadic parameter rather than elements of that array type. And then immutable(char) doesn't convert to string. I think a non-variadic overload could be added to make it work as expected.
Sep 19 2023
On Tuesday, 19 September 2023 at 19:57:34 UTC, Nick Treleaven wrote:This is because a single array can be passed to a typesafe variadic parameter rather than elements of that array type. And then immutable(char) doesn't convert to string. I think a non-variadic overload could be added to make it work as expected.https://github.com/dlang/phobos/pull/8818
Sep 19 2023
On Tuesday, 19 September 2023 at 20:20:17 UTC, Nick Treleaven wrote:On Tuesday, 19 September 2023 at 19:57:34 UTC, Nick Treleaven wrote:Thank you very much.This is because a single array can be passed to a typesafe variadic parameter rather than elements of that array type. And then immutable(char) doesn't convert to string. I think a non-variadic overload could be added to make it work as expected.https://github.com/dlang/phobos/pull/8818
Sep 19 2023