Is this actually supposed to be legal?

Philippe Sigaud philippe.sigaud at gmail.com
Wed Jul 18 07:42:22 PDT 2012


> That being said, I have never used CRTP in D so far, since template
mixins seem to be the better choice in almost all situations.

FWIW, CRTP is the main reason I used classes in Pegged, to allow grammar
rules to refer to themselves. My earlier attempts with structs did not work.

So, given a grammar rule like:

Expr <- '(' Expr ')' / ...

I use:

class Expr : Or! (Sequence!(Literal!("("), Expr, Literal!(")")) , ...)
{ ... }

As you can see, class Expr refer to itself while it's not defined yet. It's
the main use I've found for this idiom. Many C++ parsers use the same trick
and I was particularly glad to see it worked in D too.

Most of the times I use mixins, but could not find a way to do the same
recursive rule definition with them.

IIRC, I talk a bit about the CRTP in my tutorial on D templates , on Github.

Philippe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20120718/48872d80/attachment.html>


More information about the Digitalmars-d-learn mailing list