-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Introduce 'f' sigils for string interpolation #9512
Open
williamthome
wants to merge
7
commits into
erlang:master
Choose a base branch
from
williamthome:feat/f-sigil
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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