digitalmars.D.learn - Initialize static array without explicit length
- Andrey (7/8) Dec 03 2018 Hi,
- Simen =?UTF-8?B?S2rDpnLDpXM=?= (16/24) Dec 03 2018 There's no special syntax for this (even though it's been
- Dennis (7/8) Dec 03 2018 It even is in phobos:
- Simen =?UTF-8?B?S2rDpnLDpXM=?= (4/12) Dec 03 2018 The more you know. Thankies! :)
Hi, I want to create a static array and immediately init it with values:uint[xxxxx] data = [1,3,10,44,0,5000];I don't want to set the length of it explicitly (xxxxx in square brackets). I want that compiler itself counted number of values (in example it is 6). What should be a right syntax?
Dec 03 2018
On Monday, 3 December 2018 at 09:51:45 UTC, Andrey wrote:Hi, I want to create a static array and immediately init it with values:There's no special syntax for this (even though it's been requested numerous times). However, it's easy to implement in a library: import std.traits : CommonType; CommonType!T[T.length] staticArray(T...)(T args) if (is(CommonType!T)) { return [args]; } unittest { auto a = staticArray(1,2,3,4); static assert(is(typeof(a) == int[4])); } -- Simenuint[xxxxx] data = [1,3,10,44,0,5000];I don't want to set the length of it explicitly (xxxxx in square brackets). I want that compiler itself counted number of values (in example it is 6). What should be a right syntax?
Dec 03 2018
On Monday, 3 December 2018 at 10:00:31 UTC, Simen Kjærås wrote:However, it's easy to implement in a library:It even is in phobos: https://dlang.org/phobos/std_array.html#.staticArray ``` import std.array: staticArray; auto a = [0, 1, 2].staticArray; ```
Dec 03 2018
On Monday, 3 December 2018 at 12:19:26 UTC, Dennis wrote:On Monday, 3 December 2018 at 10:00:31 UTC, Simen Kjærås wrote:The more you know. Thankies! :) -- SimenHowever, it's easy to implement in a library:It even is in phobos: https://dlang.org/phobos/std_array.html#.staticArray ``` import std.array: staticArray; auto a = [0, 1, 2].staticArray; ```
Dec 03 2018