Skip to content

Base: read a number from a string (U32.of_string, F32.of_string) #1789

Closed

Description

@adeerkhan

Base can print a number (U32.show, F32.show) but cannot read one back. Every data layer needs it: a CSV cell is bytes, and "5.1" is not an F32.

What it costs today. bend-kit-f64 says it outright: "Decimal text is not here." So the number parsers live inside the packages that need them (the JSON kit, the CSV kit) and every other package that reads data writes its own. That is the whole gap.

The shape I would use, the same one Base uses for its own errors:

def U32.of_string(s: String) -> Result<&1, &1, U32 & String, U32>:
  # Fail{(code, message)} when the text is not a number

and F32.of_string(s: String) -> Result<&1, &1, U32 & String, F32> for a decimal (a sign, a fraction, and an exponent, or a documented subset of them).

The law that comes with it, and it is provable by the kernel:

law of_string_show:
  for x: U32
  {U32.of_string(U32.show(x)) == x : U32}

(The F32 round trip is not a law: F32.show rounds, so the text is not the number. The U32 one is exact.)

Alternatives, for the record: bend-kit-parse is a parser-combinator library and would do the work, and the JSON kit already has a private number parser. Both are workarounds for a missing prelude function.

I am working on a machine-learning library for Bend 2 (the data layer of scikit-learn's surface), where this is the first thing in the way, and I would be glad to send a draft pull request with the parser and the law. Say the word and I will.

Activity

  1. oxura commented on Oct 20, 2026

    @oxura
    Contributor

    These are already in Base as U32.read and F32.read, both returning Maybe. U32.read is also used in the guide’s monad example.

    I just checked the interpreter, native C (--gpu off) and emitted JS: U32.read("4294967295") succeeds, "4294967296" and "nope" return None, and F32.read("5.1") succeeds (F32.show prints 5.1). Invalid float text returns None too.

    Did you need a Result with an error message specifically, or were the existing names just hard to find? The parsers themselves are there already.

  2. Lorenzobattistela commented on Oct 21, 2026

    @Lorenzobattistela
    Collaborator

    Closing as already covered: Base has U32.read(s: String) -> Maybe<&2, U32> and F32.read(s: String) -> Maybe<&2, F32>. Both reject non-numeric text, and U32.read also rejects overflow, on every backend (see tests/base/show_read.bend and tests/base/read_bounds.bend). If you need an error message, a wrapper in your library maps None to Fail{(code, msg)}; that doesn't need a second parser in the prelude. Feel free to reopen if something these two don't cover gets in your way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions