Pick a class at random
axricard
axelrwiko at gmail.com
Thu Jan 4 09:55:38 UTC 2024
On Wednesday, 3 January 2024 at 17:44:00 UTC, H. S. Teoh wrote:
> On Wed, Jan 03, 2024 at 04:50:57PM +0000, axricard via
> Digitalmars-d-learn wrote:
>> I have an interface that is implemented by many classes, and I
>> want to pick one of these implementations at random. There are
>> two more constraints : first the distribution is not uniform,
>> all classes can define the chance they have to be picked (this
>> is reflected by the function 'weight()' below). And all
>> classes are not always available, this depends on some runtime
>> information.
>
> I would tag each implementation with a compile-time enum and
> use compile-time introspection with CRTP[1] to auto-generate
> the code for choosing a class according to the desired
> distribution.
>
> [1]
> https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern
That works very well, thank you !!
More information about the Digitalmars-d-learn
mailing list