[Issue 12484] Template Overloading or Pattern Matching Failure
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Thu Mar 27 16:33:23 PDT 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12484
bearophile_hugs at eml.cc changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |bearophile_hugs at eml.cc
--- Comment #2 from bearophile_hugs at eml.cc 2014-03-27 16:33:19 PDT ---
(In reply to comment #1)
> And I just realized that the error is mine.
In some cases it's better to ask first in D.learn.
> The extra n parameter must go at
> the end or template inference can't figure it out, of course. This works:
>
> struct Cmp(x: Zero, y: Succ!n, c: Less, n) {}
> struct Cmp(x: Succ!n, y: Zero, c: Greater, n) {}
>
>
> void main()
> {
> assert(is(Cmp!(Zero, Zero, Equal))); //Ok
> assert(is(Cmp!(Zero, One, Less))); //Ok
> assert(is(Cmp!(One, Zero, Greater))); //Ok
> }
It's better to use static asserts:
struct Zero {}
struct Succ(a) {}
alias One = Succ!Zero;
struct Less {}
struct Equal {}
struct Greater {}
enum Cmp(x: Zero, y: Zero, c: Equal) = true;
enum Cmp(x: Zero, y: Succ!n, c: Less, n) = true;
enum Cmp(x: Succ!n, y: Zero, c: Greater, n) = true;
static assert(Cmp!(Zero, Zero, Equal));
static assert(Cmp!(Zero, One, Less));
static assert(Cmp!(One, Zero, Greater));
Also take a look at the new std.traits.TemplateOf and std.traits.TemplateArgsOf
(2.066).
In D you can compare values with ==, types with is(x == y), and type
constructors (all successive ranks) with __traits(isSame, x, y).
Also take a look at the new enum/alias short syntax.
An alternative implementation mixes types and values (I have had to add a new
CMP):
struct Zero {}
struct Succ(a) {}
alias One = Succ!Zero;
alias Two = Succ!One;
enum CMP { less, equal, greater }
enum Cmp(x: Zero, y: Zero, ) = CMP.equal;
enum Cmp(x: Zero, y: Succ!n, n ) = CMP.less;
enum Cmp(x: Succ!n, y: Zero, n ) = CMP.greater;
enum Cmp(x: Succ!n, y: Succ!m, n, m) = Cmp!(n, m);
static assert(Cmp!(Zero, Zero) == CMP.equal);
static assert(Cmp!(Zero, One) == CMP.less);
static assert(Cmp!(One, Zero) == CMP.greater);
static assert(Cmp!(One, Two) == CMP.less);
--
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list