On Wednesday, 26 February 2025 at 15:11:47 UTC, bkoie wrote:
stuff like this is not even necessary if you dont need it now dont delcare an easy workaround is use some copy dictionary.

It's not that simple. I found the following code very sympathetic. You can use the same method for structures that contain dynamic arrays and can be customized as needed...

```d
alias MSS(T) = My_Static_Struct!T;
struct My_Static_Struct(Type)
{
  Type id = 0;
  Type[8] arr;

  alias T = typeof(this);
  @disable this ();

  static init(Type id)
  {
    T that = void;
    that.id = id;

    return that;
  }
}

alias MDS(T) = My_Dynamic_Struct!T;
struct My_Dynamic_Struct(Type)
{
  Type id;
  Type[] arr;

  alias T = typeof(this);
  @disable this ();

  static init(Type id, size_t length)
  {
    alias R = Type[];

    import std.range : uninitializedArray;
    T that = void;
    that.id = id;
    that.arr = length.uninitializedArray!R;

    return that;
  }
}

import std.stdio;
void main()
{
  alias T = ubyte;
  MSS!T[] m1;
  with(MSS!T)
  {
    m1 = [
      init(41),
      init(42)
    ];
  }
  m1.writefln!"%-(%s\n%)";

  MDS!T[] m2;
  with(MDS!T)
  {
    m2 = [
      init(41, 4),
      init(42, 8)
    ];
  }
  m2.writefln!"%-(%s\n%)";
}
/* PRINTS:

My_Static_Struct!ubyte(41, [0, 0, 0, 0, 0, 0, 0, 0])
My_Static_Struct!ubyte(42, [0, 0, 0, 0, 0, 0, 0, 0])
My_Dynamic_Struct!ubyte(41, [16, 32, 216, 47])
My_Dynamic_Struct!ubyte(42, [32, 32, 216, 47, 100, 127, 0, 0])
*/
```

SDB@79

Reply via email to