Skip to content

feat: add strconv stdlib module for string-to-type conversions #1603

@SchoolyB

Description

@SchoolyB

Summary

EZ doesn't have a way to convert strings to primitive types with error handling. The only option is cast(s, int) which panics on bad input. There's no fallible conversion, no string -> uint, and no string -> bool.

A strconv module fixes this with proper fallible returns and fills the gaps.

Proposed API

strconv.to_int(s string) -> (int, Error)
strconv.to_uint(s string) -> (uint, Error)
strconv.to_float(s string) -> (float, Error)
strconv.to_bool(s string) -> (bool, Error)

strconv.from_int(n int) -> string
strconv.from_uint(n uint) -> string
strconv.from_float(f float) -> string
strconv.from_bool(b bool) -> string

strconv.is_numeric(s string) -> bool
strconv.is_integer(s string) -> bool

Example

import @strconv

do main() {
    mut val, err = strconv.to_int("42")
    if err != nil {
        println("bad input")
    }

    mut s = strconv.from_int(100)

    if strconv.is_integer(userInput) {
        mut n, _ = strconv.to_int(userInput)
    }
}

Impact on cast()

cast(s, int) currently uses internal C helpers (ez_builtin_string_to_int, ez_builtin_string_to_float) that panic on bad input. These are not user-facing builtins — they're codegen internals for cast(). Once strconv lands, cast() string-to-numeric behavior can optionally be rewired to use the strconv C implementations, but cast() itself stays as-is since its semantics are "convert or panic".

Notes

  • to_int / to_uint are base-10 for now. Optional base parameter can come later
  • Follows the to_X / from_X convention used by bytes, encoding, and other stdlib modules
  • All to_X functions return (T, Error) consistent with EZ's fallible pattern

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requeststdlibGeneral standard library issues

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions