Hi everyone,
I’d like to introduce a proposal to simplify string interpolation in Erlang by extending triple-quoted strings with the f
sigil. This builds on the foundation of EEP 64 (Triple-Quoted Strings) and addresses the verbosity of dynamic content insertion.
Motivation
Currently, interpolating values into triple-quoted strings requires manual concatenation:
% Tedious today:
render(Bindings) ->
<<
"<html>\n"
" <head>\n"
" <title>", (maps:get(title, Bindings))/binary, "</title>\n"
" </head>\n"
"</html>"
>>.
AFAIK, it’s not possible to do the above using Triple-Quoted Strings.
This PR proposes a cleaner syntax using ~f"""..."""
, allowing embedded Erlang expressions enclosed in {}
:
% Proposed:
render(Bindings) ->
~f"""
<html>
<head>
<title>{maps:get(title, Bindings)}</title>
</head>
</html>
""".
Example output:
1> render(#{title => ~"Example"}).
<<"<html>\n <head>\n <title>Example</title>\n </head>\n</html>">>
Key Features
f
Sigil Syntax:~f"""..."""
evaluates to a binary with interpolated values.- Expression Handling: Values inside
{}
must be binaries (supports nesting and escaping\{
). - Alignment with EEP 64: Complements triple-quoted strings without breaking existing code.
There is EEP 62 that proposes interpolation but with a different syntax.
Feedback Welcome!
This is a draft implementation targeting OTP-28. I’d appreciate thoughts on:
- The
f
sigil vs. alternatives (e.g.,i
) - Syntax choices (
{}
vs. other delimiters) - Edge cases or potential ambiguities
Please review the PR and share your opinions here. Let’s discuss!
PR Link: