Extension: Alternate string literal syntax (mixins, DSLs)
Kristian Kilpi
kjkilpi at gmail.com
Sun Feb 18 03:12:22 PST 2007
On Sun, 11 Feb 2007 17:55:39 +0200, Kristian Kilpi <kjkilpi at gmail.com>
wrote:
>
> String literals with mixins are a bit awkward sometimes (editor
> highlighting etc).
>
> Some special marks -- I use @{ }@ here -- could be used to mark a part
> of a source file as a string literal, just like /* */ marks a part of
> code as a comment. For example:
>
> mixin(
> @{
> //this is a string literal block
> if(...) {
> ...
> }
> }@
> );
>
> The @{ }@ marks have a close relation, of course, with quotation marks
> "". But because there is a starting mark and an ending mark, you can
> nest them. (And because they are used to mark a part of a file as a
> string literal, they are not actually the part of the 'working code'
> just like the "" literals are, if you get what I'm trying to say.)
>
> E.g.
>
> alias @{
> str = @{ foo }@ ~ @{ bar }@;
> str ~= "blah";
> if(...) {
> ...
> }
> }@ MyCode;
>
> mixin(MyCode);
I think the following would also be nice (in addition to other ideas
suggested in this thread):
template MyTemplate() {...}
alias @{ ... }@ MyAlias;
@{
val = $MyTemplate!();
str = $MyAlias;
}@
And the string literal would be treaded by the compiler as:
@{
val = }@ ~ MyTemplate!() ~ @{
str = }@ ~ MyAlias ~ @{
}@
(I.e. "val = " ~ MyTemplate!() ~ "str = " ~ MyAlias ~ "")
I use $ here, but @ or some other (unambiguous) character could be used
instead, of course.
In addition, the compiler could convert numeric literals automatically to
strings:
const double v = 1.0;
int MyFunc() { return 10; }
@{ val = $MyFunc() + $v; }@
->
" val = 10 + 1.0; "
I think the $ marks inside nested @{ }@s should not be converted. For
example:
const int v = 1;
@{
val = $v;
@{
val2 = $v;
}@
}@
->
"
val = 1;
@{
val2 = $v;
}@
"
More information about the Digitalmars-d
mailing list