I’m proposing to implement heredocs, a triple-quoted text.
@josevalim describes/proposes it in Eep 59 as:
we may support triple-quoted strings, which reads better in multi-line format and reduces the need for escaping
This PR implements it. Improvements and tests are needed, but in general, it works.
For example, given this module:
-module(triple_quotes).
-export([examples/0]).
examples() ->
% Expands to <<"foo \"bar\" baz\nfizz buzz\n The indent is related to the first char">>
<<"""
foo "bar" baz
fizz buzz
The indent is related to the first char
""">>,
% Expands to "\" \"foo\" \"bar\" \"baz\" \"\n\t\"fizz\" \"buzz\""
"""
" "foo" "bar" "baz" "
"fizz" "buzz"
""",
% Expands to <<"foo">>
<<"""foo""">>,
% Expands to <<"foo\"bar\"baz">>
<<"""foo"bar"baz""">>.
And this is how the module is scanned using the code of the commit:
1> c("core_scan.erl"),io:format("~p~n", [core_scan:string(begin {ok, Bin} = file:read_file("/home/williamthome/triple_quotes.erl"), binary_to_list(Bin) end)]).
{ok,[{'-',1},
{module,1},
{'(',1},
{triple_quotes,1},
{')',1},
{'.',1},
{'-',2},
{export,2},
{'(',2},
{'[',2},
{examples,2},
{'/',2},
{integer,2,0},
{']',2},
{')',2},
{'.',2},
{examples,4},
{'(',4},
{')',4},
{'->',4},
{'<',6},
{'<',6},
{string,6,
"foo \"bar\" baz\nfizz buzz\n The indent is related to the first char"},
{'>',8},
{'>',8},
{',',8},
{string,10,"\" \"foo\" \"bar\" \"baz\" \"\n\t\"fizz\" \"buzz\""},
{',',11},
{'<',13},
{'<',13},
{string,13,"foo"},
{'>',13},
{'>',13},
{',',13},
{'<',15},
{'<',15},
{string,15,"foo\"bar\"baz"},
{'>',15},
{'>',15},
{'.',15}],
19}
The motivation is to create a more readable code and simplify the multi-line text creation.
Python and Elixir have it as well.
For me, this is a great feature.
What do you think about it?