|
| 1 | +# Tipos de Datos |
| 2 | + |
| 3 | +**Groovy** es un lenguaje con tipado opcional, lo que significa que podemos trabajar con tipos `primitivos de Java`, o podemos dejar que el `runtime` los deduzca del contexto, lo cuál lo hace bastante bien. |
| 4 | + |
| 5 | +* `x` es claramente un número |
| 6 | +* Un lenguaje opcionalmente tipado |
| 7 | +* Sin ninguna definición de tipo |
| 8 | + |
| 9 | +Veamos cuales son los tipo primitivos que vamos a utilizar: |
| 10 | + |
| 11 | +| Data Type | Groovy Keyword | Sample Data | |
| 12 | +|:---------:|:--------------:|---------------| |
| 13 | +| Strings | String | "Jaime Salas" | |
| 14 | +| Integers | int | 0, 1, 2, 3 | |
| 15 | +| Floats | float | 0.5, 3.8 | |
| 16 | +| Boolean | Boolean | true, false | |
| 17 | + |
| 18 | +# Demo: Tipos de Datos |
| 19 | + |
| 20 | +Creamos el siguiente fichero [01_groovy_data_types.groovy](playground/01_groovy_data_types.groovy) |
| 21 | + |
| 22 | +```groovy |
| 23 | +String name = "Joe Doe" |
| 24 | +int courseCount = 14 |
| 25 | +float salary = 999999.99 |
| 26 | +Boolean isProgrammer = true |
| 27 | +
|
| 28 | +println name + " has created " + courseCount + " courses." // [1] |
| 29 | +println name + " is a programmer? " + isProgrammer // [1] |
| 30 | +println name + " wishes his salary was " + salary // [1] |
| 31 | +``` |
| 32 | + |
| 33 | +1. Groovy convierte los `booleans` y `float` a `strings` |
| 34 | + |
| 35 | +Y lo podemos ejecutar de la siguiente manera: |
| 36 | + |
| 37 | +```bash |
| 38 | +$ docker run --rm -v $(pwd):/home/groovy/scripts -w /home/groovy/scripts groovy:latest groovy 01_groovy_data_types.groovy |
| 39 | +Joe Doe has created 14 courses. |
| 40 | +Joe Doe is a programmer? true |
| 41 | +Joe Doe wishes his salary was 1000000.0 |
| 42 | +``` |
| 43 | + |
| 44 | +Vamos a introducir una pequeña diferencia |
| 45 | + |
| 46 | +```diff |
| 47 | +String name = "Joe Doe" |
| 48 | +int courseCount = 14 |
| 49 | +float salary = 999999.99 |
| 50 | +Boolean isProgrammer = true |
| 51 | + |
| 52 | +println name + " has created " + courseCount + " courses." |
| 53 | +-println name + " is a programmer? " + isProgrammer |
| 54 | ++println name + " is a programmer? " + isProgrammer.toString().capitalize() |
| 55 | +println name + " wishes his salary was " + salary |
| 56 | +``` |
| 57 | + |
| 58 | +```bash |
| 59 | +$ docker run --rm -v $(pwd):/home/groovy/scripts -w /home/groovy/scripts groovy:latest groovy 01_groovy_data_types.groovy |
| 60 | +Joe Doe has created 14 courses. |
| 61 | +Joe Doe is a programmer? True |
| 62 | +Joe Doe wishes his salary was 1000000.0 |
| 63 | +``` |
| 64 | + |
| 65 | +Podemos notar que `Groovy` tiene un problema al convertir de `float` a `string`, podemos tener un control más fino usando el formateo de `String`. |
| 66 | + |
| 67 | +```diff |
| 68 | +String name = "Joe Doe" |
| 69 | +int courseCount = 14 |
| 70 | +float salary = 999999.99 |
| 71 | +Boolean isProgrammer = true |
| 72 | + |
| 73 | +println name + " has created " + courseCount + " courses." |
| 74 | +println name + " is a programmer? " + isProgrammer.toString().capitalize() |
| 75 | +-println name + " wishes his salary was " + salary |
| 76 | ++println name + " wishes his salary was \$" + String.format("%.2f", salary) |
| 77 | +``` |
| 78 | + |
| 79 | +```bash |
| 80 | +$ docker run --rm -v $(pwd):/home/groovy/scripts -w /home/groovy/scripts groovy:latest groovy 01_groovy_data_types.groovy |
| 81 | +Joe Doe has created 14 courses. |
| 82 | +Joe Doe is a programmer? True |
| 83 | +Joe Doe wishes his salary was $1000000.00 |
| 84 | +``` |
| 85 | + |
| 86 | +Seguimos teniendo un problema, simplemente podemos cambiar el tipo: |
| 87 | + |
| 88 | +```diff |
| 89 | +String name = "Joe Doe" |
| 90 | +int courseCount = 14 |
| 91 | +-float salary = 999999.99 |
| 92 | ++BigDecimal salary = 999999.99 |
| 93 | +Boolean isProgrammer = true |
| 94 | + |
| 95 | +println name + " has created " + courseCount + " courses." |
| 96 | +println name + " is a programmer? " + isProgrammer.toString().capitalize() |
| 97 | +-println name + " wishes his salary was \$" + String.format("%.2f", salary) |
| 98 | ++println name + " wishes his salary was \$" + salary |
| 99 | +``` |
| 100 | + |
| 101 | +Simplemente como nota en `Java` tenemos `;` para terminar una línea, en `Groovy` son opcionales. |
| 102 | + |
| 103 | +### Tipos de Datos y Sintaxis |
| 104 | + |
| 105 | +* "def" opcional o tipo de dato explicito |
| 106 | +* ¿Perder el tipado? Nop |
| 107 | +* Ciando devolvemos un valor está perfectamente claro el tipo devuelto |
0 commit comments