Operator overloading

aarti_pl aarti at interia.pl
Tue Dec 23 14:13:27 PST 2008


Denis Koroskin pisze:
> Slightly off-topic.
> 
> How would you implement, say, LIKE condition? Similar to the following, perhaps:
> Query query = Select(a).Where(id.Like("%substring%"));

Well, probably:

...Like(id, "substring")...

In this case I prefer this kind of syntax more than OO kind of syntax :-)

> You could implement "==" the same way:
> Query query = Select(a).Where(id.EqualsTo(5));

In fact LIKE is not as a big problem as other operators. You will rather 
not use nested LIKEs, but only the simplest form.

The best solution would be to introduce into language concept of infix 
operator, so that any symbol could be such an operator. It would be most 
universal solution. But it would be probably too much to ask...

Completely different situation than with LIKE is with comparison and 
logical operators. They form very complicated expressions eg.:

Where(Or(And(Equals(surname, "Smith"), NotEquals(age, 25))), 
And(Equals(surname, "Neo"), NotEquals(age, 50)))

With my syntax it would be:
Where((surname == "Smith" && age != 25) || (surname == "Neo" && age != 50))

I don't know how to write it using your syntax, because I don't have 
good idea for AND/OR.

But the most important argument is that syntax, where operators are 
parts of classes will cause unnecessary coupling of classes and concepts.

In my case objects which are used to identify columns are universal. 
Using same identifiers you can also get values from resulting table.
When I implement parts of expression in Column classes I will loose this 
decoupling: someone who just wants my resulting table object + column 
identifiers will have to see api for sql expressions, which he/she will 
not use at all.

Best Regards
(aarti_pl)



More information about the Digitalmars-d mailing list