[Issue 14593] operator overloading can't be used with expression templates

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Tue Jun 16 18:33:03 PDT 2015


https://issues.dlang.org/show_bug.cgi?id=14593

Igor Stepanov <wazar.leollone at yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |wazar.leollone at yahoo.com

--- Comment #2 from Igor Stepanov <wazar.leollone at yahoo.com> ---
(In reply to Martin Nowak from comment #0)
> A motivating example is using expression templates to specify db queries.
> 
> db.get!Person.where!(p => p.age > 21 && p.name == "Peter")
> 
> This has the following problems:
> - opCmp doesn't allow to capture the op
> - it's not possible to overload &&
> - opEquals must return a bool (but here it needs to return p)
> 
> A solution would be to use the generic opBinary for < <= > >= == iff
> opCmp/opEquals don't exist and also extending it to && ||.

(In reply to Walter Bright from comment #1)
> Using expression templates to implement DSL's makes code really hard to
> understand and debug. There are better ways.

+1
You may pass where condition as string, and use libdparse, or something like it
to build AST and compile it to your SQL expression.
db.get!Person.where!(`p.age > 21 && p.name == "Peter"`)
This looks not worse, and at the same time, more honest.
BTW, if we will implement .astof property, which returns AST of
expression/symbol (without possibility to generate new symbol from existing AST
it will not be macros), we will able to use compiler to generate AST of this
expression:

template AstOf(string expr)
{
    enum AstOf = mixin(expr).astof;
}

--


More information about the Digitalmars-d-bugs mailing list