alisinabh
Best way to achieve empty string
So in elixir sometimes you need to do something like this:
def function(acc, condition1, condition2) do
acc <>
cond do
condition1 -> "hello"
condition2 -> "world!"
true -> "" # Add nothing to acc
end
end
Sometimes you need to use an empty string. Currently i only see two options for that
- using
""(empty string with two double qoutes) - using
<<>>(empty binary)
It would be a good thing if Elixir’s String module would provide a function like: String.empty/0 that would return a human readable form of empty string in Elixir.
But for now, Which way you think is better to use? 1 or 2? Or any other solutions?
I personally think that 2 is better since no special chars can hide inside it!
Most Liked
rvirding
Have you never used binaries for protocols? ![]()
OvermindDL1
Heh, now that Unicode is out in full force, that is not at all a bad point, hmm…
OvermindDL1
Network protocols! Bitstring syntax in network protocols in Erlang is an utter bliss compared to any other language I’ve used! ^.^
Like parse out the TCPv4 packet format from scratch, just for fun, then try it in any other language. ^.^
alisinabh
In Elixir code they mostly used "" as Empty string.
NobbZ
I’m not sure if we really need String.empty/0. Thats very noisy and writing the empty string literally saves me a lot of keystrokes.
And if one should use "" vs. <<>>, well it depends. If I am dealing with String (meaning a sequence of unicode characters) I do prefer "" while I prefer to use <<>> when I am dealing with random binary garbdata.







