to_int(), to_bool(), to_float(), to_byte(), to_str()

to_int(), to_bool(), to_float(), to_byte(), to_str()

Luma includes a clean and predictable type conversion system using method-like syntax. These methods allow you to convert values safely, with optional default fallbacks.

Core Philosophy

Luma’s type conversions:

  • Are explicit (value.to_int() instead of implicit coercion)
  • Support both nullable and default behavior
  • Avoid panics or crashes on bad conversions
  • Follow consistent naming: to_type() and to_type(default)

Quick Reference

String → Primitive

Method Returns Description
s.to_int() ?int Parse integer, nil on failure
s.to_int(default) int Parse integer, fallback on failure
s.to_float() ?float Parse float, nil on failure
s.to_float(default) float Parse float, fallback on failure
s.to_bool() ?bool Parse boolean, nil on failure
s.to_bool(default) bool Parse boolean, fallback on failure
s.to_byte() ?byte Parse hex/decimal byte, nil on failure
s.to_byte(default) byte Parse hex/decimal byte, fallback on failure

Any → String

Method Returns Description
v.to_str() str Convert any value to string (always succeeds)

Cross-type

Method Returns Description
b.to_int() int Convert a byte to its integer value (0–255)
s.to_bytes() [byte] Convert a string to its UTF-8 byte list
data.to_str() str Convert a [byte] to a UTF-8 string

For byte list encoding/decoding methods (to_hex, to_base64, from_hex, etc.), see String Utilities — Encoding and Lists — Byte List Methods.

For integer ↔ byte list conversions (to_int16, to_int24, to_int32), see Lists — Integer Conversion.

Integer Conversion

name: str = "123"

x: ?int = name.to_int()      // nullable, returns int or nil
y: int = name.to_int(0)      // fallback default
  • "42" → 42
  • "abc" → nil
  • " 7 " → 7 (whitespace is trimmed)
  • "-3" → -3

Float Conversion

"3.14".to_float()        // 3.14
"invalid".to_float()     // nil
"5.2".to_float(0.0)      // 5.2

Note: Luma uses the dot (.) as the decimal separator (no locale-dependent comma)

Boolean Conversion

"true".to_bool(false)    // true
"FALSE".to_bool(true)    // false
"0".to_bool(false)       // false
"1".to_bool(false)       // true
  • Case-insensitive
  • Accepts "true", "false", "1", "0"
  • Anything else returns nil (or the fallback default)

Byte Conversion

Parses a string into a single byte value (0–255). Supports hex and decimal formats:

"0x48".to_byte()         // 72
"72".to_byte()           // 72
"0XFF".to_byte()         // 255
"invalid".to_byte()      // nil
"999".to_byte()          // nil (out of range)

With a fallback default:

"invalid".to_byte(0x00)  // 0

String Conversion

age: int = 27
s: str = age.to_str()        // "27"

pi: float = 3.14
s = pi.to_str()              // "3.14"

flag: bool = true
s = flag.to_str()            // "true"

Works on any value, including optional or nil.

Byte → Integer

A byte value can be converted directly to int:

b: byte = 0x41
n: int = b.to_int()      // 65

This returns the numeric value of the byte (0–255), not a string parse.

String → Byte List

Convert a string to its raw UTF-8 bytes:

data: [byte] = "Hello".to_bytes()
// [72, 101, 108, 108, 111]

The inverse is data.to_str() on a [byte] — see Lists — Byte List Methods.

Nullable vs Fallback

Every string-to-primitive conversion supports two forms:

Form Return type On failure
s.to_int() ?int returns nil
s.to_int(0) int returns the default
val: str = "xyz"

a: ?int = val.to_int()     // nil
b: int  = val.to_int(0)    // 0

yes: bool = "TRUE".to_bool(false)   // true
no: bool = "oops".to_bool(false)    // false

x: ?byte = "ZZ".to_byte()           // nil
y: byte = "ZZ".to_byte(0x00)        // 0

Use the nullable form when you need to distinguish “missing” from “zero”. Use the fallback form when you always need a valid value.

Notes

  • All string-to-primitive conversions trim whitespace before parsing
  • to_str() never fails — it works on any type
  • byte.to_int() is a direct numeric cast, not a string parse
  • to_bytes() and [byte].to_str() are UTF-8 conversions
  • No auto-coercion (like JS or Python) — all conversions are explicit
  • No locale-sensitive parsing (no "1,23" float)
Last updated on