Type Conversion
Quetzal provides explicit conversion methods for transforming values between types.
Conversion Methods
Section titled “Conversion Methods”| Method | Description | Example |
|---|---|---|
.texto() | Convert any value to text | (42).texto() returns "42" |
.entero() | Convert text to integer | "123".entero() returns 123 |
.numero() | Convert text to decimal number | "3.14".numero() returns 3.14 |
.log() | Convert text to boolean | "verdadero".log() returns verdadero |
.lista() | Convert text to list | "1,2,3".lista() |
.jsn() | Convert text to JSON object | '{"a":1}'.jsn() |
Examples
Section titled “Examples”To Text
Section titled “To Text”entero numero = 42texto como_texto = numero.texto() // "42"
lista<entero> numeros = [1, 2, 3]texto lista_texto = numeros.texto() // "[1, 2, 3]"
jsn datos = { nombre: "Ana" }texto json_texto = datos.texto()texto json_formateado = datos.texto_formateado()To Numbers
Section titled “To Numbers”texto cadena_entero = "123"entero valor_entero = cadena_entero.entero() // 123
texto cadena_decimal = "3.14159"numero valor_decimal = cadena_decimal.numero() // 3.14159To Boolean
Section titled “To Boolean”texto verdad = "verdadero"log valor_logico = verdad.log() // verdadero
texto falsedad = "falso"log valor_falso = falsedad.log() // falsoError Handling
Section titled “Error Handling”intentar { entero valor = "not a number".entero()} capturar (excepcion error) { consola.mostrar_error("Conversion failed: " + error.mensaje)}Implicit Conversions
Section titled “Implicit Conversions”Quetzal does not perform implicit conversions between types. You must explicitly convert values when needed:
entero edad = 25// This would fail: "Age: " + edad// Correct way:texto mensaje = "Age: " + edad.texto()Best Practices
Section titled “Best Practices”- Always validate input before conversion
- Use
intentar/capturarfor external or user-provided data - Prefer explicit conversions for code clarity