digitalmars.D.learn - Variadic function not recognizing static array length
- Zach the Mystic (12/12) Mar 12 2013 void func(string[2] a) {}
- Zach the Mystic (5/19) Mar 12 2013 I'm just going to assert at runtime instead of at compile time.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (10/31) Mar 12 2013 Actually, this is not about variadic function templates. The type of an
- Zach the Mystic (6/46) Mar 13 2013 Thanks. I see that the type is a slice, but it doesn't stop
- Timon Gehr (4/17) Mar 13 2013 http://d.puremagic.com/issues/show_bug.cgi?id=9712
- Zach the Mystic (4/27) Mar 14 2013 Man, Hara Kenji is so fast!!!!!! I didn't even know my problem
- Zach the Mystic (2/32) Mar 14 2013 (I meant to thank you too, Timon Gehr)
- Steven Schveighoffer (8/20) Mar 14 2013 Actually an array literal's default type is a slice, not a fixed-sized
void func(string[2] a) {} void func2(T...)(T args) { static assert(is(typeof(args[0]) == string[2])); } void func3(T...)(T args) { static assert(args[0].length == 2); } func(["",""]); // Okay func2(["",""]); // Error: (is(string[] == string[2LU])) is false func3(["",""]); // Error: _param_0 cannot be read at compile time Is this the intended design? Is there a workaround which allows me to ensure that the parameter is exactly 2 length?
Mar 12 2013
On Tuesday, 12 March 2013 at 21:47:02 UTC, Zach the Mystic wrote:void func(string[2] a) {} void func2(T...)(T args) { static assert(is(typeof(args[0]) == string[2])); } void func3(T...)(T args) { static assert(args[0].length == 2); } func(["",""]); // Okay func2(["",""]); // Error: (is(string[] == string[2LU])) is false func3(["",""]); // Error: _param_0 cannot be read at compile time Is this the intended design? Is there a workaround which allows me to ensure that the parameter is exactly 2 length?I'm just going to assert at runtime instead of at compile time. assert(args[0].length == 2); That's a good workaround, only a minor inconvenience it can't be caught at compile time.
Mar 12 2013
On 03/12/2013 04:10 PM, Zach the Mystic wrote:On Tuesday, 12 March 2013 at 21:47:02 UTC, Zach the Mystic wrote:Actually, this is not about variadic function templates. The type of an array literal is a slice: static assert(is(typeof(["",""]) == string[])); This doesn't help you but it is possible to cast: static assert(is(typeof(cast(string[2])["",""]) == string[2])); or use a variable with an explicit type: string[2] var = ["", ""]; static assert(is(typeof(var) == string[2])); Alivoid func(string[2] a) {} void func2(T...)(T args) { static assert(is(typeof(args[0]) == string[2])); } void func3(T...)(T args) { static assert(args[0].length == 2); } func(["",""]); // Okay func2(["",""]); // Error: (is(string[] == string[2LU])) is false func3(["",""]); // Error: _param_0 cannot be read at compile time Is this the intended design? Is there a workaround which allows me to ensure that the parameter is exactly 2 length?I'm just going to assert at runtime instead of at compile time. assert(args[0].length == 2); That's a good workaround, only a minor inconvenience it can't be caught at compile time.
Mar 12 2013
On Tuesday, 12 March 2013 at 23:35:28 UTC, Ali Çehreli wrote:On 03/12/2013 04:10 PM, Zach the Mystic wrote:Thanks. I see that the type is a slice, but it doesn't stop func(string[2]) from taking ["",""] at compile time. I think it might have been felt to be problematic not to allow it. I think it's inconsistent though. Might be harder to include in complex parameter deduction than in explicit parameter lists.On Tuesday, 12 March 2013 at 21:47:02 UTC, Zach the Mysticwrote:falsevoid func(string[2] a) {} void func2(T...)(T args) { static assert(is(typeof(args[0]) == string[2])); } void func3(T...)(T args) { static assert(args[0].length == 2); } func(["",""]); // Okay func2(["",""]); // Error: (is(string[] == string[2LU])) istimefunc3(["",""]); // Error: _param_0 cannot be read at compileallows me toIs this the intended design? Is there a workaround whichtime.ensure that the parameter is exactly 2 length?I'm just going to assert at runtime instead of at compileassert(args[0].length == 2); That's a good workaround, only a minor inconvenience it can'tbe caughtat compile time.Actually, this is not about variadic function templates. The type of an array literal is a slice: static assert(is(typeof(["",""]) == string[])); This doesn't help you but it is possible to cast: static assert(is(typeof(cast(string[2])["",""]) == string[2])); or use a variable with an explicit type: string[2] var = ["", ""]; static assert(is(typeof(var) == string[2])); Ali
Mar 13 2013
On 03/12/2013 10:47 PM, Zach the Mystic wrote:void func(string[2] a) {} void func2(T...)(T args) { static assert(is(typeof(args[0]) == string[2])); } void func3(T...)(T args) { static assert(args[0].length == 2); } func(["",""]); // Okay func2(["",""]); // Error: (is(string[] == string[2LU])) is false func3(["",""]); // Error: _param_0 cannot be read at compile time Is this the intended design?Yes.Is there a workaround which allows me to ensure that the parameter is exactly 2 length?http://d.puremagic.com/issues/show_bug.cgi?id=9712 http://d.puremagic.com/issues/show_bug.cgi?id=9711
Mar 13 2013
On Wednesday, 13 March 2013 at 22:14:26 UTC, Timon Gehr wrote:On 03/12/2013 10:47 PM, Zach the Mystic wrote:Man, Hara Kenji is so fast!!!!!! I didn't even know my problem wasn't just part of the design, and already it's close to being a fixed bug/enhancement!void func(string[2] a) {} void func2(T...)(T args) { static assert(is(typeof(args[0]) == string[2])); } void func3(T...)(T args) { static assert(args[0].length == 2); } func(["",""]); // Okay func2(["",""]); // Error: (is(string[] == string[2LU])) is false func3(["",""]); // Error: _param_0 cannot be read at compile time Is this the intended design?Yes.Is there a workaround which allows me to ensure that the parameter is exactly 2 length?http://d.puremagic.com/issues/show_bug.cgi?id=9712 http://d.puremagic.com/issues/show_bug.cgi?id=9711
Mar 14 2013
On Thursday, 14 March 2013 at 20:59:38 UTC, Zach the Mystic wrote:On Wednesday, 13 March 2013 at 22:14:26 UTC, Timon Gehr wrote:(I meant to thank you too, Timon Gehr)On 03/12/2013 10:47 PM, Zach the Mystic wrote:Man, Hara Kenji is so fast!!!!!! I didn't even know my problem wasn't just part of the design, and already it's close to being a fixed bug/enhancement!void func(string[2] a) {} void func2(T...)(T args) { static assert(is(typeof(args[0]) == string[2])); } void func3(T...)(T args) { static assert(args[0].length == 2); } func(["",""]); // Okay func2(["",""]); // Error: (is(string[] == string[2LU])) is false func3(["",""]); // Error: _param_0 cannot be read at compile time Is this the intended design?Yes.Is there a workaround which allows me to ensure that the parameter is exactly 2 length?http://d.puremagic.com/issues/show_bug.cgi?id=9712 http://d.puremagic.com/issues/show_bug.cgi?id=9711
Mar 14 2013
On Tue, 12 Mar 2013 17:47:00 -0400, Zach the Mystic <reachzach gggggmail.com> wrote:void func(string[2] a) {} void func2(T...)(T args) { static assert(is(typeof(args[0]) == string[2])); } void func3(T...)(T args) { static assert(args[0].length == 2); } func(["",""]); // Okay func2(["",""]); // Error: (is(string[] == string[2LU])) is false func3(["",""]); // Error: _param_0 cannot be read at compile time Is this the intended design? Is there a workaround which allows me to ensure that the parameter is exactly 2 length?Actually an array literal's default type is a slice, not a fixed-sized array. With IFTI, the type is decided by the call, not how you use it the parameters. I filed an enhancement request a while back that would help fix this. http://d.puremagic.com/issues/show_bug.cgi?id=4998 -Steve
Mar 14 2013