idi527

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

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe
iex(7)> """
...(7)> foo\
...(7)> """
"foo"
18
Post #4
thbar

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

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.

OvermindDL1

OvermindDL1

Uh, if Elixir worked that way I’d be fine with it, but it doesn’t, and based on the way Elixir’s does work I’d prefer Swift’s way as well:

╰─➤  iex
Erlang/OTP 20 [erts-9.2] [source] [64-bit] [smp:6:6] [ds:6:6:10] [async-threads:10] [hipe] [kernel-poll:false]

Interactive Elixir (1.6.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> """
...(1)> Foo"""
** (SyntaxError) iex:2: invalid location for heredoc terminator, please escape token or move it to its own line: """

iex(1)> """Foo"""
** (SyntaxError) iex:1: heredoc start must be followed by a new line after """

the heck forum syntax coloring borked ^.^;

NobbZ

NobbZ

Hmmm? Did they remove this syntax? I think I used it in some old code… Perhaps I’m misremembering things though…

Where Next?

Popular in Questions Top

belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New

Other popular topics Top

belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement