Domain Specific Languages in D; was: C++, D: Dinosaurs?
Aarti_pl
aarti at interia.pl
Tue Nov 4 07:32:09 PST 2008
Julio César Carrascal Urquijo pisze:
> Hello Aarti_pl,
>
>> 3. Impossibility to statically control meta language contructs.
>>
>> Sql has its own syntax rules and currently they can not be enforced on
>> compile time.
>>
>> E.g. in select statement you would expect to have OrderBy after Where,
>> not other way.
>
> Instead of returning the full query object you could return a "Fragment"
> query. Something like this:
>
> class WhereFragment
> {
> this(Query q) {}
> WhereFragment And(Expression e) {}
> OrderByFragment OrderBy(Column[] columns) {}
> }
Yes, something like this would be possible. But it would start to be
very complicated, unmanageable and not so useful soon. Implementing it
in such a way for every DSL will be very hard. Also there are cases in
normal programs where it would be useful to have such checks.
Currently I use simple runtime checks (somewhat simplified, pseudocodish
and not tested code below):
class SelectStatement {
string[] next;
this() {
next = ["Where", "From"]; //All possible options after construction
}
public SelectStatement Where(SqlExpression exp) {
if ("Where" !in next) throw Exception("Syntax error");
...
next = ["OrderBy"];
}
}
Probably it should be possible to implement something similar in
compiler to make checks on compile time. If some variable has assigned
object of such a class compiler should trace this variable and check if
methods are called properly (in order, that they *are* called etc.)
I don't know how easy / difficult is to implement something like this in
compiler, but I think it may be difficult.
>
>> Currently I can enforce order of clauses only on
>> runtime, although it should be probably doable to enforce it on
>> compile time. Currently it is also not possible to enforce that e.g.
>> some method must be called:
>>
>> Query query = Select(id).From(table);
>>
>> Imagine that you want to enforce that in above query From() must be
>> always called (Select is an object, From is method on this object).
>
> You should probably do as LINQ to SQL and start the query with the FROM
> statement instead. That way you can cascade the type and help
> auto-complete in IDEs.
Well, I don't see problem here with my approach... SQL has a lot of
options after SELECT keyword and before WHERE. How can I use them using
only From?
> But yes, I don't think it's currently possible to
> enforce a method to be called.
BR
Marcin Kuszczak
(aarti_pl)
More information about the Digitalmars-d
mailing list