gleam-lang / gleam-lang/stdlib

Suggestion: Add `decode.number`

Open
#801 12 comments 0 reactions 0 assignees View on GitHub
Dominant language
Gleam
Stars
710
Forks
225
Avg merge
5h 52m
Merged PRs (30d)
3

Description

## Context

Given that
1. the `decode` API is [used with formats like JSON](https://github.com/gleam-lang/json), where there is no distinction between floats and ints, and
2. the target (Erlang vs ES) is a somewhat leaky abstraction when it comes to handling numbers,

it can be confusing that on the Erlang target `decode.float` will not accept e.g. `1`, but requires `1.0` to work properly.

## Suggestion

Add `decode.number: Decoder(Float)`, which would work equivalently to
```gleam
decode.one_of(decode.float, or: [decode.int |> decode.map(int.to_float)])
```

This would both make it more convenient to decode formats where there's no distinction between ints and floats, and act as documentation about the context outlined above.

If this sounds like a good idea, I'd be happy to create a PR for it.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.