-
Notifications
You must be signed in to change notification settings - Fork 3k
Introduce 'f' sigils for string interpolation #9512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
CT Test Results 2 files 97 suites 1h 8m 39s ⏱️ Results for commit e1fabd7. ♻️ This comment has been updated with latest results. To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass. See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally. Artifacts// Erlang/OTP Github Action Bot |
c625f3c
to
1eb9678
Compare
This commit introduces a strict way of generating payloads using the `f` sigils. It constructs a binary and only accepts binaries as dynamic values. For example: ```erlang 1> User = #{name => ~"Bob", age => 27}. 2> ~f""" Name: {maps:get(name, User)} Age: {integer_to_binary(maps:get(age, User))} """. <<"Name: Bob\nAgen: 27">> ```
This commit adds more flexibility to the `f` sigil by introducing more sigil prefixes that output strings. For example: ```erlang 1> User = #{name => ~"Bob", age => 27}. 2> ~fs""" Name: {maps:get(name, User)} Age: {integer_to_binary(maps:get(age, User))} """. ["Name: ",<<"Bob">>,"\nAge: ",<<"27">>] ``` The `f` sigil will not raise an exception for string modifiers if the dynamic value is not a string or a binary. For example: ```erlang 1> User = #{name => 'Bob', age => 27}. 2> ~fs""" Name: {maps:get(name, User)} Age: {maps:get(age, User)} """. ["Name: ",'Bob',"\nAge: ",27] ```
This commit changes texts to be UTF-8 binaries instead of lists. For example: ```erlang 1> User = #{name => ~"Bob", age => 27}. 2> ~fs""" Name: {maps:get(name, User)} Age: {integer_to_binary(maps:get(age, User))} """. [<<"Name: ">>,<<"Bob">>,<<"\nAge: ">>,<<"27">>] ```
4217865
to
e841efd
Compare
It's not easy to determine the exact column of the forms. This commit changes to only store the line as the location to avoid confusion and misleadings. This commit also removes the unneeded text in the strings anno.
I have been pondering this for a while, and while I can't really put my finger on it, I have a bad feeling about string interpolation and bringing it in the language 🤷♂️ |
This PR introduces
~f
sigils for ergonomic string interpolation, enabling cleaner binary/iolist generation while reducing boilerplate and errors.Key Features
~f
: Returns a flattened binary (ideal for in-memory processing).fs
: Returns an iolist (ideal for I/O operations).Note
Currently, Erlang has those sigils:
s
,S
,b
, andB
. Following up on all of them, I've also implemented:fb
,fB
,bf
orBf
: is the same asf
.fS
,sf
orSf
: is the same asfs
.Syntax
{}
within the string.{
with a backslash\
to treat it as a literal.Minimal Example
Why This Is a Good Thing
Developer Experience
<<...>>
/++
juggling.Flexibility
~f
) or iolists (~fs
) based on use case.Backward-Compatible
Examples
HTML Templating (
~f
for Binaries)Logging (
~fs
for IOList Efficiency)Edge Cases
Escaping: Use
\{
for literal{
.Nesting: Combine
~f
and~fs
freely.Discussion
No major drawbacks come to mind with this approach, but feedback is welcome.
It would be fantastic to include this feature in OTP-28!
Please see the Erlang Forums thread for more details and discussion.
Related Links