Skip to content

Typing violation in lib/xlsx_reader/conversion.ex when using Decimal.parse/1 #40

@tosie

Description

@tosie

The Elixir 1.18 compiler outputs the following warning when using xlsx_reader 0.8.8:

     warning: the following clause will never match:

         {:ok, decimal}

     because it attempts to match on the result of:

         Decimal.parse(string)

     which has type:

         dynamic(
           :error or {%Decimal{coef: :NaN or :inf, exp: integer(), sign: integer()}, binary()} or
             {%Decimal{coef: :NaN or :inf or integer(), exp: integer(), sign: integer()}, term()}
         )

     typing violation found at:
     │
 155 │       {:ok, decimal} ->
     │       ~~~~~~~~~~~~~~~~~
     │
     └─ (xlsx_reader 0.8.8) lib/xlsx_reader/conversion.ex:155: XlsxReader.Conversion.to_decimal/1

Here is the affected code:

def to_decimal(string) do
case Decimal.parse(string) do
{:ok, decimal} ->
{:ok, decimal}
{decimal, ""} ->
{:ok, decimal}
_ ->
:error
end
end

The (documentation of Decimal.parse/1)[https://hexdocs.pm/decimal/Decimal.html#parse/1]) describes the possible outputs as:

If successful, returns a tuple in the form of {decimal, remainder_of_binary}, otherwise :error.

It seems like the typing change was introduced with Decimal 2.0 in September of 2020 (https://github.com/ericmj/decimal/releases/tag/v2.0.0).

Not sure how to solve this (= not showing a warning) without breaking backwards compatibility to Decimal 1.x ... have you thought about how to handle this, yet? Maybe drop support for older versions of Decimal? Or would you just ignore the compiler warning?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions