Compile-Time Sort in D

Seb via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Mon Jun 5 14:35:54 PDT 2017


On Monday, 5 June 2017 at 14:23:34 UTC, Mike Parker wrote:
> The crowd-edited (?) blog post exploring some of D's 
> compile-time features is now live. Thanks again to everyone who 
> helped out with it.
>
> The blog:
> https://dlang.org/blog/2017/06/05/compile-time-sort-in-d/
>
> Reddit:
> https://www.reddit.com/r/programming/comments/6fefdg/compiletime_sort_in_d/

This is a great article, Mike!
At the end I expected a reference to D's great template 
constraints [1], maybe it's still worth adding sth. like this to 
show how amazingly useful CTFE is?

auto myRandomEngine(ulong m, ulong a, ulong c)(ulong seed)
if (properLinearCongruentialParameters!(m, a, c))
{
   return seed;
}

void main()
{
     static assert(!__traits(compiles, myRandomEngine!(1, 2, 
3)(42)));
     myRandomEngine!(1UL << 32, 1664525, 1013904223)(42);
}

Or alternatively if you don't want to rewrite 
properLinearCongruentialParameters e.g.

auto myRandomEngine(ulong m, ulong a, ulong c)(ulong seed)
if (pLCP!(m, a, c))
{
   return seed;
}

template pLCP(ulong m, ulong a, ulong c) {
     enum pLCP = properLinearCongruentialParameters(m, a, c);
}

[1] https://dlang.org/spec/template.html#template_constraints


More information about the Digitalmars-d-announce mailing list