Defining a static array with values in a range
Vlad Levenfeld via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jan 21 22:09:22 PST 2015
On Thursday, 22 January 2015 at 05:56:40 UTC, tcak wrote:
> I want to define alphanumeric characters in an easy way.
> Something like that:
>
> char[] arr = ['a'..'z', 'A'..'Z', '0'..'9'];
>
> Though above example doesn't work. Is there any easy way to do
> this?
>
> I am trying to do something like EBNF definitions. So, I do not
> want to use loops, or define every single thing one by one by
> hand.
This is interesting, I think this syntax would be pretty cool if
it did work (I think it would work in foreach loops... at least
it does with integers).
Anyway, what you could do is define a static struct with slicing
overloaded for chars, and have it return slices of a master char
array or a generating function. Assume you called the struct C,
then your char array declaration could looks something like
join (C['a'..'z'], C['A'..'Z'], C['0'..'9'])
It is a bit of up-front work, though it is maintainable. I'm also
curious if there is indeed an easier way.
More information about the Digitalmars-d-learn
mailing list