I’m working on a SQL query builder module and I’d like to provide a fluent API using method chaining (https://en.wikipedia.org/wiki/Fluent_interface). My goal was something like:
After a bit of struggle because multiple dispatch, I ended up with what seems like a more Julian approach, overloading the
select(...) + from(...) + where(...) + limit(...)
Wondering if there’s a better way to implement method chaining in Julia?
However, when the API is used, it becomes verbose again as I have to prefix the methods with the module name (I don’t want to export the methods as the names are very common and would lead to name clashes – nor am I happy with
const QB = QueryBuilder, still ugly). So I end up with:
QueryBuilder.select(...) + QueryBuilder.from(...) + QueryBuilder.where(...) + QueryBuilder.limit(...)
My solution is to provide a syntax similar to VisualBasic’s
with blocks (https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/with-end-with-statement#example) - something like:
with QueryBuilder .select(...) + .from(...) + .where(...) + .limit(...) end
Is it possible to write a similar
@with macro in Julia? My first attempts failed because Julia complains when I try to build an expression which starts with a dot:
ERROR: syntax: invalid identifier name ".". So I’m not sure how to attack this…
Thanks for your feedback, much appreciated!