Thursday, March 22, 2012

Indented strings and struct annotations

I've just checked in support for two new features: indented strings and
struct annotations.

Indented strings allow you to write string constants across multiple lines
without breaking indentation:

    string_val := I'here is a document
                    embedded in a string definition
                        - this line will be indented in the output
                    but the rest will not.';


This would produce the following output:

    here is a document
    embedded in a string definition
        - this line will be indented in the output
    but the rest will not.

In an indented string, the escaped newline also escapes all following

    # string_val = "this is a single line string!"
    string_val := I"this is a \
                    single line string!";

In an indented string (indicated with a capital "I" prefix) the tokenizer
strips all leading whitespace after a newline up to the minimum level of
indentation following a newline for the entire string. Whitespace after an
escaped newline, though ignored in the string itself, is considered in
determining the minimum indentation level. So if you wanted to define a string
containing a list of indented lines, you could do something like this:

    string_val := I'  a) item 1
                      b) item 2
                      c) item 3\

The escaped newline and the whitespace before the final quote will be
ignored, but forces the minimum indentation to two characters before the start
of the previous lines.

This trick also works for i-strings:

    cout I`Hello!

Struct annotations essentially allow you to define a class consisting only of
instance variables with a constructor that allows you to initialize all of the
instance variables. So the Coord example from the manual (defining a two
dimensional coordinate) can now be written like this:

    @import crack.ann struct;

    @struct Coord {
        int x, y;

    c := Coord(10, 20);  # create a Coord with x = 10, y = 20

The struct annotation is still very limited: you can't use inheritance or
define methods, and there are no auto-generated formatTo() or comparison
methods. These niceties will eventually be added. But it's still a worthwhile

No comments:

Post a Comment