Defining a static array with values in a range

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jan 22 07:03:22 PST 2015


On Thursday, January 22, 2015 10:42:59 bearophile via Digitalmars-d-learn wrote:
> Jonathan M Davis:
>
> > auto r = chain(uiota('a', 'z'), uiota('A', 'Z'), uiota('0',
> > '9'));
>
> Those ranges are probably open on the right.

They probably are actually, since open on the right is usually how things
are done, but that's easy fixed with some +1's.

> Kagamin:
>
> > If you declare the string as immutable, the concatenation will
> > be done at compile time.
>
> In function-scope I think you need enum.

Yeah. immutable has nothing to do with compile time, though if you're
talking about concatenating constants, there's a decent chance that they'd
be optimized so that no concatenation occurs at runtime. However, to force
anything to happen at compile time, you need to be initializing something
that _has_ to be initialized at compile time (enum, static variable, direct
initialization of  member variable, etc.). If the compiler ever has a
choice, it won't do it aside from built-in stuff that it understands well
enough to translate as an optimization.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list