If your project is already using Ecto, you can also use
Ecto.Type.cast/2:

iex(2)> Ecto.Type.cast(:integer, 42) {:ok, 42} iex(3)>
Ecto.Type.cast(:integer, "42") {:ok, 42}
(same for floats, dates, etc)

Probably best to wrap it in a more convenient function like this though:

defmodule MyApp do def cast!(type, term) do {:ok, casted} =
Ecto.Type.cast(type, term) casted end end


On January 12, 2022, "José Valim" <jose.va...@dashbit.co> wrote:
> Hi Curtis,
> All functions in String accept a string only as first argument, so I
> would be wary of making an exception. We could perhaps allow it in
> Integer.parse/2 but I am not sure if we should make an exception there
> either, especially when we consider there are other arguments such as
> base and what they should mean when an integer is given.
> On Wed, Jan 12, 2022 at 03:05 Curtis Rock <curtis.r...@gmail.com
> <mailto:curtis.r...@gmail.com>> wrote:
> > I propose that String.to_integer/1 return an integer rather than
> > raising an error when passed an integer argument.
> >
> > Use case: When working with Phoenix LiveView and HEEx templates,
> > database table id values are processed as both binary values and
> > integer values, but must be converted back to integer when writing
> > to the database.
> >
> > When String.to_integer/1 is passed an integer value, it raises an
> > error.
> >
> > We add functions in our projects to make string to integer
> > conversion more forgiving, such as the following functions found in
> > the elixir library
> >
> >  defp to_integer(integer) when is_integer(integer), do: integer
> >
> >  defp to_integer(integer) when is_binary(integer), do:
> > String.to_integer(integer)
> >
> > sourced from here:
> > <https://github.com/elixir-
> > lang/elixir/blob/79cd891eb86ecb7654a7acdb63769cfdd950a5c0/lib/ex_unit/lib/ex_unit/filters.ex#L238>
> >
> >
> >  --
> >  You received this message because you are subscribed to the Google
> > Groups "elixir-lang-core" group.
> >  To unsubscribe from this group and stop receiving emails from it,
> > send an email to elixir-lang-core+unsubscr...@googlegroups.com
> > <mailto:elixir-lang-core+unsubscr...@googlegroups.com>.
> >  To view this discussion on the web visit
> > https://groups.google.com/d/msgid/elixir-lang-core/2fdcbff9-5551-
> > 4e91-aea1-68fc7447adfan%40googlegroups.com
> > <https://groups.google.com/d/msgid/elixir-lang-core/2fdcbff9-5551-
> > 4e91-aea1-
> > 68fc7447adfan%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
>  -- 


>  You received this message because you are subscribed to the Google
> Groups "elixir-lang-core" group.
>  To unsubscribe from this group and stop receiving emails from it,
> send an email to elixir-lang-core+unsubscr...@googlegroups.com
> <mailto:elixir-lang-core+unsubscr...@googlegroups.com>.
>  To view this discussion on the web visit
> https://groups.google.com/d/msgid/elixir-lang-
> core/CAGnRm4J0rxCk7vK%3D1r%2BM9As7UV8n7JXHt0g4t%3DuMeLB2YVBFjw%40mail.gmail.com
> <https://groups.google.com/d/msgid/elixir-lang-
> core/CAGnRm4J0rxCk7vK%3D1r%2BM9As7UV8n7JXHt0g4t%3DuMeLB2YVBFjw%40mail.gmail.com?utm_medium=email&utm_source=footer>.

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elixir-lang-core+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/cc2203ff2675428c7bbf2c8d0a0da95cebc9e997%40hey.com.

Reply via email to