Distinguishing less than / greater than usage in a DSL


I’m working on a DSL for writing queries that’s part of a database access library. I’d like to be able to do something like:

SomeDatabaseModel.query {
    name == "abcd"
    x > 5

for most operators this is straightforward to do by using query builders that define operator functions that can record what operator was called and on what arguments.

However, since <, >, <=, >= are all translated to .compareTo(), my builder can’t distinguish which operator is being used in order to incorporate that into the query.

At the moment, I’m just working around this with infix functions called lt and gt, etc., but I would prefer to use the operators if possible. Is there any way to define these operators independently of one another, or somehow distinguish inside .compareTo() which operator is being used? (The query builders are eventually going to be autogenerated code anyway, so I’m ok with low-level, codegen-based or complex solutions here if there’s any workaround available at all.)



I know the problem, you actually forget one. The == operator does not work either due to compiler special casing. The approach I have (in my database access library - https://github.com/pdvrieze/kotlinsql) is to just use: lt, le, eq, ne, ge, gt. In my library all the operations are translated to actual sql, not executed client side.