Reputation: 5805
I want compile-time (because to be used in static if
) comparison of version strings in D.
For example 1.2.12
is greater than 1.2.2
. I want to do such comparisons compile-time.
Upvotes: 1
Views: 53
Reputation: 1228
Write a function that works at runtime. Then call it. Something like:
bool less(string a, string b)
{
auto ap = a.splitter(".").map!(x => to!int(x));
auto bp = b.splitter(".").map!(x => to!int(x));
while (!ap.empty && !bp.empty)
{
if (ap.front < bp.front) return true;
ap.popFront; bp.popFront;
}
return ap.empty;
}
static if (less("1.2.12", "1.2.2"))
pragma(msg, "it's less");
else
pragma(msg, "it's not less");
You can call normal functions at compile time. This is what in D we call CTFE (compile time function evaluation).
To quote Walter Bright (from my memory) "D has the unique ability to run D code at compile time".
Upvotes: 3