srand in D

Ali Çehreli acehreli at yahoo.com
Sun Jan 9 03:15:02 UTC 2022


On 1/8/22 4:17 PM, kdevel wrote:

 >     enum immutable (char) [] allowed_chars = [
 >        'c', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'r', 't', 'v',
 >        'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9'
 >     ];
 >
 >     char q = uniform!allowed_chars;
 >     writeln (q);
 >
 > complains with
 >
 > uni2.d(11): Error: template instance `uniform!(['c', 'f', 'g', 'h', 'j',
 > 'k', 'l', 'm', 'n', 'p', 'r', 't', 'v', 'w', 'x', 'y', 'z', '1', '2',
 > '3', '4', '5', '6', '7', '8', '9'])` has no value

That's because uniform's first template parameter is expected to be a 
type. (There are two uses of 'enum' in D and the one above is not a 
type, rather "this is a manifest constant".)

One might think that uniform should work with a string like "hello" but 
in that case what are the choices? The single string or its individual 
characters?

What would work in the code above is 'choice':

   char q = choice(allowed_chars);

But that hits another fact of D: arrays of chars are strings, which 
cannot be RandomAccessRange because individual chars must be decoded to 
form dchars.

Keeping the 'choice' line above and replacing two 'char's with 'dchar's 
in your code makes it work.

Ali



More information about the Digitalmars-d-learn mailing list