inferred size for static array initialization

Marco Leise via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 2 06:53:22 PDT 2016


Am Mon, 02 May 2016 13:00:27 +0000
schrieb Erik Smith <erik at cruiserhouse.com>:

> Is there a way to initialize a static array and have it's size 
> inferred (and that works for arrays of structs using braced 
> literals)?  This would make it easier to maintain longer static 
> array definitions.  The code below doesn't work when removing the 
> array size even though the array is declared as static immutable.
> 
>      import std.traits;
>      static immutable int[] a  = [1,2,3];
>      static assert(isStaticArray!(typeof(a)));  // fails
> 

Sure,

  struct S { int a, b; }

  immutable tab = { static enum S[] s = [
      {1,2},
      {3,4},
  ]; return cast(typeof(s[0])[s.length])s; }();

  static assert(isStaticArray!(typeof(tab)));  // succeeds

-- 
Marco



More information about the Digitalmars-d-learn mailing list