Overloading relational operators separately; thoughts?

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Wed Sep 28 13:16:08 PDT 2016


On 9/28/2016 6:58 AM, Timon Gehr wrote:
>> An excellent example of that is the std.regex package.
> It's actually a bad example, because it is irrelevant: it is obviously a bad
> idea to implement regex using operator overloading, because the regex operators
> have no D equivalent.

Yet this "obviously bad" idea regularly resurfaces as a cool use of expression 
templates and respected people in the industry like it and advocate it.


> Assume I have two symbolic expressions a and b:
>
> Expression a=variable("a"), b=variable("b");
>
> Why should I be allowed to do
>
> auto c = a + b; // symbolic value a + b
>
> but not
>
> auto d = a <= b; // symbolic value a <= b

Because there is no way to stop the former but still have operator overloading.


> The string parsing approach is not useful here: how do I use existing
> expressions that are accessible in the scope to build new expressions?

You seem to be asking for expression templates. They do actually work in D, but 
I recommend against them.

To reiterate, operator overloading exists in D to support the inclusion of 
arithmetic library types. Any other purpose is discouraged, and that includes 
expression templates and things like << for iostreams.

AST macros are an alternative to expression templates. Having AST macros has 
been proposed before multiple times, and has been debated at length. I haven't 
seen any new information that would justify reopening the issue.


More information about the Digitalmars-d mailing list