Using enforce or assert to check for fullness when appending to fixed length array container

Per Nordlöw per.nordlow at gmail.com
Fri Oct 4 10:00:08 UTC 2019


I have a wrapper container FixedArray at

https://github.com/nordlow/phobos-next/blob/25f4a4ee7347427cebd5cd375c9990b44108d2ef/src/fixed_array.d

on top of a static array store that provides the member

     void insertBack(Es...)(Es es) @trusted
     if (Es.length <= capacity) // TODO use `isAssignable`
     {
         import std.exception : enforce;
         enforce(_length + Es.length <= capacity, `Arguments don't 
fit in array`); // TODO use assert insteead?

         foreach (immutable i, ref e; es)
         {
             moveEmplace(e, _store[_length + i]); // TODO remove 
`move` when compiler does it for us
         }
         _length = cast(Length)(_length + Es.length); // TODO 
better?
     }

Is the usage of `enforce` to check for out of bounds (fullness) 
idiomatic D or should an `assert()` be used instead?


More information about the Digitalmars-d-learn mailing list