Index an AliasSeq with a run-time index

ag0aep6g anonymous at example.com
Sun Aug 14 10:41:20 UTC 2022


On 14.08.22 12:08, Per Nordlöw wrote:
> How do I index an `AliasSeq` with an integer known at run-time?

With a `switch` that has a `case` for every possible index:

----
import std.meta: AliasSeq;
alias seq = AliasSeq!("foo", "bar", "baz");
string f(size_t rti)
{
     sw: switch (rti)
     {
         static foreach (cti; 0 .. seq.length)
         {
             case cti: return seq[cti]; break sw;
         }
         default: break sw;
     }
     return "";
}
----


More information about the Digitalmars-d-learn mailing list