Fl4m3Ph03n1x
Background
I want to convert a String to an integer. To this extent I know I can use one of the two following functions:
String.to_integerInteger.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?
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
- #blog-post
- #phoenix_html
- #ai
- #iex
- #graphql
- #elixirconf-us
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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
What makes
Integer.parseexplode? 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.
NobbZ
If there can be at least one digit be parsed to the given base, you’ll get
{integer, rest},:errorotherwise, rasie should only happen if thebaseis invalid.Fl4m3Ph03n1x
I respect your opinion, but I would like to direct you to the official Elixir documentation, in specific to this part:
Source: https://elixir-lang.org/getting-started/try-catch-and-rescue.html#errors
I am aware of this. My objective here is to find out if there is a parsing function in Elixir that doesn’t throw and always returns a tagged tupple. I understand this is not the case, correct?
NobbZ
There is none, but you can easily wrap yourself, as I have shown in my code example.
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_integerin atry. 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"toString.to_integer. Just like it should raise if it is passed:potato.Elixir consistently raises
ArgumentErrorfor 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 anArgumentErrorif thebaseis 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, likeKeyError)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?Ted
I have a related question.
(Well, I think it might be related; I’m a novice and everything’s possible.
)
I see that Elixir’s naming conventions discuss the trailing-bang:
So, in a parallel Elixir universe, could
Integer.parsenot exist and instead we’d have bothString.to_integerandString.to_integer!?EDIT: hmm, maybe that parallel universe would actually be a pain; I overlooked the fact that
Integer.parseaccounts forremainder_of_binaryandString.to_integerdoesn’t.