x-lang

← Index

x/type/convert

Generic type conversion: the Convert class over the type system’s from/to alists.

Hot consumers fetch the dispatcher from the catalogue: (prim-ref ‘convert ‘to). The no-match policy is the (Convert missing) static field.

The base type handles have a public spelling: (Type named INTEGER), (Type named STRING), and so on for any registered name.

Class Convert

missing

No-match policy handler (fn (_ val target)). The dialect sets it – raise, coerce, log, … Default returns nil.

Static field: data held by Convert itself, not by its instances.

(Convert to val target . extra)

Convert VAL to the TARGET type via the type system’s registered conversions.

Parameters:

Returns: ANY — The converted value; on no registered conversion, (Convert missing)’s result (default nil)

Examples:

(Convert to "42" (Type named INTEGER)) => 42
(Convert to 255 (Type named STRING) 16) => "ff"

(Convert to-int v what)

V as an INT: V itself when it is one, else its registered conversion to INT; raises WHAT when there is none.

Parameters:

Returns: INTEGER — The integer

Examples:

(Convert to-int 7 "no") => 7
(Convert to-int "12" "no") => 12
(guard (e e) (Convert to-int 'x "not an int")) => "not an int"