Fl4m3Ph03n1x

Fl4m3Ph03n1x

String to integer tuple API

Background

I want to convert a String to an integer. To this extent I know I can use one of the two following functions:

  1. String.to_integer
  2. Integer.parse

My concern

My concern here is the API. With option 1 if the string is invalid, it will just raise an exception and explode. This is not very elixiry as it forces me down to a try/rescue approach.

So the alternative would be option 2. Except the API for this function is rather inconsistent. Sometimes it will return :error other times it will just explode. Quite literally, I do not know what to expect, so I still need to use a try/rescue here as well.

Question

My objective is to have a parsing function that returns a tagged tuple for the errors. Something among the lines of {:error, reason}. Now, I know I can easily create one, but I was wondering if there is a function in Elixir that already does this.

Is there any parse function in Elixir that already returns tagged tupples when parsing data?

Marked As Solved

NobbZ

NobbZ

There is none, but you can easily wrap yourself, as I have shown in my code example.

Also Liked

jola

jola

I believe you’re misunderstanding the intent of that paragraph. It is correct that errors are not used as control flow, and that’s why the function explicitly says “must be the string representation of an integer”. The paragraph supports this, you shouldn’t wrap String.to_integer in a try. That’s the intent of the paragraph, not that errors are not used at all. Rather they should be used for “unexpected and/or exceptional situations”, such as someone passing "hello" to String.to_integer. Just like it should raise if it is passed :potato.

Elixir consistently raises ArgumentError for invalid inputs in the cases where the input doesn’t make sense at all.

Another great example of this is of course Integer.parse, which accepts any string, because it’s made specifically for unknown string inputs, while it throws an ArgumentError if the base is incorrect.

Digging deeper into the Elixir source can be helpful here, where you’ll find lots of instances of raising ArgumentError (there are also other errors used regularly, like KeyError)

jola

jola

For 1. I disagree with you, I consider it perfectly elixiry. The function is for data that you know can be parsed as an integer. You should not use it for unknown inputs. The documentation is pretty clear

string must be the string representation of an integer. Otherwise, an ArgumentError will be raised. If you want to parse a string that may contain an ill-formatted integer, use Integer.parse/1 .

What makes Integer.parse explode? It should give you either a tuple of number and remainder or :error.

Ah, I see it raises if you give it an invalid base, not because you give it an invalid string input.

hauleth

hauleth

Will you really allow user to specify base? Or you will really use base over 36? In general almost all Elixir functions can throw when passed incorrect arguments. Do you also want to prevent situation when someone passes atom to Enum.reverse/1?

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
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
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

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 29377 241
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement