idi527
How I would create a multiline string without a trailing line break with Elixir?
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir
iex(2)> """
...(2)> asdf
...(2)> """
"asdf\n"
I wonder how I would create a multiline string without a trailing line break with elixir.
In swift I can add a line break by adding a newline
let lineBreaks = """
This string starts with a line break.
It also ends with a line break.
"""
which is the same as
line_breaks = """
This string starts with a line break.
It also ends with a line break.
"""
in elixir.
I feel like swift’s way is a bit more consistent and gives freedom to chose how to end the string.
Is there a reason for elixir’s multiline string to be different? I see only one advantage to this approach:
ab = """
a
b
"""
c = """
c
"""
ab <> c == """
a
b
c
"""
In swift it would be
"""
a
bc
"""
but can be fixed by adding a new line at the end of ab.
Most Liked
benwilson512
thbar
In case someone stumbles on this thread: the “\” at the end to avoid newline still appears to work with recent versions of Elixir.
iex(1)> """
...(1)> foo\
...(1)> """
"foo"
iex(2)> """
...(2)> foo
...(2)> """
"foo\n"
NobbZ
"""
Foo"""
# or even
"""Foo"""
I prefer the elixir behaviour over the swift one, as elixir complies with POSIX that text files should always end in a newline.
Last Post!
przemyxe0p
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #elixirconf-us
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









