alisinabh
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!
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
OvermindDL1
Heh, now that Unicode is out in full force, that is not at all a bad point, hmm…
alisinabh
In Elixir code they mostly used
""as Empty string.https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/string.ex#L440
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 binarygarbdata.Qqwy
As string combining (
{"", &<>/2}) forms a Monoid (Just like:{0, &+/2},{1, &*/2},{false, &Kernel.||/2}{[], &++/2},{%{}, &Map.merge/2},{MapSet.new(), &MapSet.union/2}et cetera…
) , both
String.empty/0andString.append/2would be wonderful functions to have, making this behaviour more explicitNobbZ
By that argumentation we also need
Integer.one/0,Integer,zero/0,Integer.multiply/2,Integer.plus/2,List.empty/0,List.append/2,Map.empty/0, et cetera.Monoids are true in elixir as they are in haskell, but not such a big thing. Also the monoid just works, regardles of using
String.empty/0or"".I have to admit, sometimes one wants to/has to pass a function which returns the neutral of a monoid and since writing
&("")or&""/1isn’t possible, we have to usefn -> "" end, but that cases are so rare, that I do not think that there is really a benefit of adding such functions.NobbZ
Also I have jsut discovered that there has been a PR closed by José:
https://github.com/elixir-lang/elixir/pull/6235
alisinabh
No, I think you didn’t got my point.
There are UTF-8 chars that are not visible. Like half space in Arabic and etc.
Since i write code with Persian strings in it, there is a common mistake that i press
alt+Spacethat generates a half space which is not visible.Here is an ordinary double quote →
""Here is a half space between two double quotes →
""Can you tell which one is the real empty one?
This problem is not affecting integers or list nor any other data type.[quote=“NobbZ, post:7, topic:6203”]
Also I have jsut discovered that there has been a PR closed by José:
[/quote]
As @josevalim commented in this PR today:
He suggested to tackle this issue by using a good string inspection that shows these things.
NobbZ
You diodn’t got whom I answered
That was in direct resonse to @Qqwy comment about beeing
{"", &<>/2}a monoid.But in general, I think you are right, invisible characters are indeed a problem, but in my opinion, they are not a problem of a language. They are a problem of your editor and the display engine used therein.
Just consider C, they don’t have a function which creates an empty string, but can run into even worse trouble than we could when there are random invisible characters in their strings when they don’t expect them.
alisinabh
Sorry about that
I’m seeking an atom package that shows whitespaces.
I just ran into this issue yesterday and it was frustrating to find out about that whitespace in my string.
Since i was a .NET developer before Elixir and back there it has
string.Emptyconstant i thought maybe it’s a good idea here too.But since that whitespace can happen in any other strings force showing them in editor will be a good idea. And since there are not lots of people using a keyboard with this kind of layout adding
String.empty/0does not make sense.Thank’s
OvermindDL1
That is because C does not have standard strings, it has char-array’s at best.
C++ on the other hand does have an empty string, it is called
string().As well as a lot of C and C++ libraries that have strings do indeed add an
emptyglobal constant string variable.I like seeing invisible chars, but I’ve not seen one that shows invisible unicode characters yet, if you find one that shows that, please tell me!