Raw String Literals - Proposal Based on Assignment Rather Delimiter

Bruno Borges bruno.borges at gmail.com
Thu Jan 3 02:09:42 UTC 2019


After reading through the summary posted by Brian Goetz on the
amber-spec-experts list [1], I started wondering if the answer to Raw
String Literals in Java perhaps should be about special assignment rather
special delimiters.

By special assignment, I mean the compiler would identify a raw string
literal based on how a variable is assigned, or constructed, and how the
statement ends.

Variables would be assigned in a different manner for a raw/multi-line
string literal:

1. For non-Raw/ML Strings, use normal assign sign `=`
2. For Raw/ML Strings, use `:` next to the variable name. Example: `text:`.
The Raw String literal ends when the construct `:variable` (colon followed
by the name of the variable) is found before the semicolon that ends the
statement, or a parenthesis closing a block such as a method call, or
annotation.

    var json: {
        "firstName": "Bruno"
    } :json;

    var sql:

SELECT column1, column2, ...

FROM table_name

WHERE condition;

    :sql;


In both examples above, variables `json` and `sql` can receive method
calls, such as align() or toUpperCase():

    var yaml:

# An employee record

name: Martin D'vloper

skill: Elite

foods:

    - Apple

    :yaml.align();


For passing parameters to methods using multi-line strings, one proposal is
to utilize the reserved word `const`, since there may be no need for
assigning a variable name. Here's an example:

conn.query(const:

SELECT column1, column2, ...

FROM table_name

WHERE condition

:const);


The soft variable `const` could still be used to call String methods (e.g.
split(regex)).

If the developer does wants to assign, or reassign the Raw/ML String to an
existing variable, he/she can do so by using the existing variable name:

var sql = "SELECT * FROM TABLE";
...

conn.query(sql:

SELECT column1, column2, ...

FROM table_name

WHERE condition

:sql);


This structure could allow the use of Raw/ML Strings within @Annotations as
well:

@Sql(const:

SELECT column1, column2, ...

FROM table_name

WHERE condition

:const)


Perhaps the core advantage of this solution is that the variable name is
repeated at the end, therefore reminding the developer what is the name of
the variable of perhaps such long text, as well as allowing the developer
to make method calls to that variable right after construct.

This design is likely to allow for other needs such as concatenation as we
discuss further this proposal based on special assignment rather special
delimiter. For concatenation, perhaps the next discussion would be how to
support templates inside Raw/ML strings.

[1]
https://mail.openjdk.java.net/pipermail/amber-spec-experts/2019-January/000931.html

Best regards,
Bruno Borges


More information about the amber-dev mailing list