Error while generate DNA with uniform()
Steven Schveighoffer
schveiguy at gmail.com
Sat Sep 3 14:25:48 UTC 2022
On 9/3/22 8:09 AM, Salih Dincer wrote:
> Hi All,
>
> We discovered a bug yesterday and reported it:
>
> https://forum.dlang.org/thread/mailman.1386.1662137084.31357.digitalmars-d-bugs@puremagic.com
>
>
> You know, there is `generate()` depend to `std.range`. It created the
> error when we use it with the value of an enum. Which get their values
> from an `enum DNA`, we have 4 members that we want to generate 32 pieces
> randomly like this:
>
> ```d
> import std;
> void main()
> {
> enum DNA { timin = 84,
> sitozin = 67,
> guanin = 71,
> adenin = 65
> }
> char[] gene;
> enum n = 32;
> auto range = generate!(() => uniform(DNA.min, DNA.max)).take(n);/*
> auto preferred = generate!(() =>
> uniform!"[]"(DNA.min,
> DNA.max)).take(n);//*/
I'm not sure why this doesn't work. First, I would change your enum to
this (for correctness and readability):
```d
enum DNA : char { timin = 'T',
sitozin = 'C',
guanin = 'G',
adenin = 'A'
}
```
There is probably a bug in generate when the element type is an `enum`
which somehow makes it const. But what you need anyway is a `char`, so
just return a `char`. For that, you need to specify the return type,
which requires a different kind of function literal:
```d
auto preferred = generate!(function char() =>
uniform!"[]"(DNA.min,
DNA.max)).take(n);
```
That works.
-Steve
More information about the Digitalmars-d-learn
mailing list