Crystal Notes: Types
Published: 2nd of August 2021
Strings
- Strings in Crystal are are declared with double quotes ("").
- Strings are stored as as sequence of UTF-8 encoded characters.
- Strings are immutable.
crystal
# Define a string.
stuff = "stuff"
# Multi-line string.
stuff_and_things="stuff
and
things"
Symbols
- Symbols in Crystal are declared by prefixing a colon (:) to a variable.
- Every instance of a Symbol is the same object. Symbols are stored as a unique Int32 value, which saves memory compared to strings.
- Symbols are immutable.
- Symbols are evaluated at runtime.
crystal
# Define a symbol.
stuff = :stuff
# Define a symbol enclosed in double quotes.
# This allows for a space in the symbol.
things = :"my things"
# Symbols are a good options for hash keys.
stuff_and_things = {
:stuff => "my stuff",
:things => "my things"
}
Boolean
Numbers
Union Types
Links
https://pragprog.com/titles/crystal/programming-crystal/
https://crystal-lang.org/reference/syntax_and_semantics/literals/string.html
https://crystal-lang.org/reference/syntax_and_semantics/literals/symbol.html