3.3. Soft-failing string-to-numeric conversions returning Result<T; ConversionError>

The STRINGS_CONVERT module provides soft-failing string-to-numeric conversions that return Result<T; ConversionError> instead of panicking or silently returning zero. Use these when parsing untrusted input where you need to distinguish between not-a-number, overflow, and trailing-garbage.

All functions and symbols are in the “strings_convert” module, use require to get access to it.

require daslib/strings_convert

Example:

require daslib/strings_convert

[export]
def main() {
    let r = try_to_int("42")
    if (is_ok(r)) {
        print("parsed: {unwrap(r)}\n")
    }
    let bad = try_to_int("nope")
    if (is_err(bad)) {
        print("error: {unwrap_err(bad)}\n")
    }
}
// output:
// parsed: 42
// error: invalid_argument

3.3.1. Enumerations

ConversionError

enum ConversionError

3.3.2. Soft-failing conversions

try_to_double(str: string ): tuple<_is_ok:bool;_value:double;_error:ConversionError>

def try_to_double (str: string) : tuple<_is_ok:bool;_value:double;_error:ConversionError>

Arguments:
  • str : string implicit

try_to_float(str: string ): tuple<_is_ok:bool;_value:float;_error:ConversionError>

def try_to_float (str: string) : tuple<_is_ok:bool;_value:float;_error:ConversionError>

Arguments:
  • str : string implicit

try_to_int(str: string; hex: bool = false ): tuple<_is_ok:bool;_value:int;_error:ConversionError>

def try_to_int (str: string; hex: bool = false) : tuple<_is_ok:bool;_value:int;_error:ConversionError>

Arguments:
  • str : string implicit

  • hex : bool

try_to_int16(str: string; hex: bool = false ): tuple<_is_ok:bool;_value:int16;_error:ConversionError>

def try_to_int16 (str: string; hex: bool = false) : tuple<_is_ok:bool;_value:int16;_error:ConversionError>

Arguments:
  • str : string implicit

  • hex : bool

try_to_int64(str: string; hex: bool = false ): tuple<_is_ok:bool;_value:int64;_error:ConversionError>

def try_to_int64 (str: string; hex: bool = false) : tuple<_is_ok:bool;_value:int64;_error:ConversionError>

Arguments:
  • str : string implicit

  • hex : bool

try_to_int8(str: string; hex: bool = false ): tuple<_is_ok:bool;_value:int8;_error:ConversionError>

def try_to_int8 (str: string; hex: bool = false) : tuple<_is_ok:bool;_value:int8;_error:ConversionError>

Arguments:
  • str : string implicit

  • hex : bool

try_to_uint(str: string; hex: bool = false ): tuple<_is_ok:bool;_value:uint;_error:ConversionError>

def try_to_uint (str: string; hex: bool = false) : tuple<_is_ok:bool;_value:uint;_error:ConversionError>

Arguments:
  • str : string implicit

  • hex : bool

try_to_uint16(str: string; hex: bool = false ): tuple<_is_ok:bool;_value:uint16;_error:ConversionError>

def try_to_uint16 (str: string; hex: bool = false) : tuple<_is_ok:bool;_value:uint16;_error:ConversionError>

Arguments:
  • str : string implicit

  • hex : bool

try_to_uint64(str: string; hex: bool = false ): tuple<_is_ok:bool;_value:uint64;_error:ConversionError>

def try_to_uint64 (str: string; hex: bool = false) : tuple<_is_ok:bool;_value:uint64;_error:ConversionError>

Arguments:
  • str : string implicit

  • hex : bool

try_to_uint8(str: string; hex: bool = false ): tuple<_is_ok:bool;_value:uint8;_error:ConversionError>

def try_to_uint8 (str: string; hex: bool = false) : tuple<_is_ok:bool;_value:uint8;_error:ConversionError>

Arguments:
  • str : string implicit

  • hex : bool