Skip to content

Literals

Strings are immutable by default, mapping to System.String. StringBuilder (System.Text.StringBuilder) is available via normal .NET interop with no special syntax.

Standard strings with #{} interpolation and escape sequences:

"Hello, #{name}!"
"Tab:\there, newline:\nhere"
"Quote: \""

Indexing a string by position returns a Char; slicing with a range returns a String:

"hello"[0] # Char — 'h'
"hello"[1..3] # String — "ell"

Single quotes are for Char (System.Char) only. A single-quoted literal holds exactly one character, or one escape sequence:

'a' # Char
'Z' # Char
'\n' # Char — newline (escape sequences are recognised)
'\\' # Char — backslash
'ab' # compile error — single quotes hold one character; use "ab" for a String

There are no single-quoted strings. For a string without interpolation or escape processing, use a raw string or a <<~'MSG' heredoc.

Triple-quoted strings — no escape processing, no interpolation. Useful for regex patterns, file paths, and embedded content. Leading and trailing newlines are stripped, and common indentation is removed (like C#’s raw string literals):

pattern = """
\d{4}-\d{2}-\d{2}
"""
json = """
{
"name": "Alice",
"age": 30
}
"""

Ruby-style squiggly heredoc — strips leading indentation to match the least-indented line. Supports interpolation:

message = <<~MSG
Hello, #{name}!
Your score is #{score}.
MSG

No-interpolation heredoc:

template = <<~'MSG'
No #{interpolation} here.
MSG
Sequence Meaning
\n Newline
\t Tab
\r Carriage return
\\ Backslash
\" Double quote
\' Single quote
\uXXXX Unicode code point
\xXX Hex byte
\0 Null byte

Regex literals use slash syntax and map to System.Text.RegularExpressions.Regex:

pattern = /\d{4}-\d{2}-\d{2}/
email = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

Regex literals are static — no #{} interpolation. For dynamic patterns, use Regex.new(string) directly.

Flags follow the closing slash and use .NET semantics:

Flag .NET equivalent Meaning
i IgnoreCase Case-insensitive matching
m Multiline ^ and $ match line boundaries
s Singleline . matches newline characters
x IgnorePatternWhitespace Whitespace ignored; # comments allowed
/hello/i # case-insensitive
/^start/m # multiline anchors
/.*body/s # dot matches newline
match = /\d+/.match("abc123") # Match? — nil if no match
match = "abc123".match(/\d+/) # same
/\d+/.matches?("abc123") # Bool
"abc123def456".match_all(/\d+/) # Array[Match]

Match wraps System.Text.RegularExpressions.Match:

m = /(\d+)-(\d+)/.match("2024-03")
m[0] # "2024-03" — full match
m[1] # "2024" — first capture group
m[2] # "03" — second capture group
m[:year] # named capture group (?<year>\d+)
m.start # start index in original string
m.end # end index

Capture groups are numbered as .NET numbers them: unnamed groups first, left to right, then named groups. In /(?<year>\d+)-(\d+)/, m[1] is the unnamed second group and m[2] is year.

"hello world".gsub(/\w+/) { |m| m.upcase } # "HELLO WORLD"
"hello world".sub(/\w+/, "goodbye") # "goodbye world"
"one two three".split(/\s+/) # ["one", "two", "three"]

Lightweight interned identifiers, backed by Amethyst.Runtime.Symbol:

:name
:user_id
:"complex symbol with spaces"

[1, 2, 3] # Array[Int]
["alice", "bob"] # Array[String]
[1, 2.5] # Array[Float] — Int widened to Float

The element type is the common type of the elements after numeric widening. Elements with no common type are a compile error; annotate the target to widen it explicitly:

[1, "one"] # compile error — no common type
items: Array[Any] = [1, "one"] # ok

{ name: "Alice", role: "admin" } # Hash[Symbol, String] — `key:` makes a symbol key
{ "alice" => 1, "bob" => 2 } # Hash[String, Int] — `=>` takes a key of any type

Keys and values are typed by the same common-type rule as array elements.

Directly after a method name, { starts a block, as in Ruby. Pass a hash literal in parentheses instead: configure({ debug: true }).


An empty collection literal [] requires the type to be inferable from context. When it cannot be inferred, use Array[T].new or the of suffix:

# Inferred from context
names: Array[String] = []
# Explicit — both forms are equivalent
names = Array[String].new
names = [] of String

The of suffix is also valid on hash literals:

lookup = {} of Symbol => String

Ruby-style %w and %i for compact list literals:

%w[alice bob charlie] # Array[String] — equivalent to ["alice", "bob", "charlie"]
%i[name age email] # Array[Symbol] — equivalent to [:name, :age, :email]

42 # Int
42_000_000 # Int with separators
3.14 # Float
0xFF # Hex Int
0b1010 # Binary Int
0o777 # Octal Int

Numeric literals take C#’s type suffixes, case-insensitively:

Suffix Type Example
u UInt 42u
l Long 42L
ul, lu ULong 42UL
f Single 3.14f
d Float 3d
m Decimal 19.99m

Suffixes combine with hex, binary, octal and separators (0xFFu, 1_000L). Prefer the upper-case L, which is harder to mistake for 1.


1..10 # inclusive range — Range
1...10 # exclusive range — Range (end not included)

Ranges are an Amethyst type, not System.Range: System.Range has no inclusive form, cannot be enumerated, and stores Index values rather than numbers. Range values are value types. start and end are both Int:

r = 1..10
r.start # 1
r.end # 10 (included)

Ranges support array and string slicing using the standard subscript syntax:

array[2..5] # elements at indices 2, 3, 4, 5
string[0...3] # first three characters

Ranges are enumerable — each, map, to_array, etc. are available via the Enumerable mixin.

.NET-style hat (^) indices address from the end of a collection. These are valid in range literals and subscripts:

array[^1] # last element — System.Index
array[1..^1] # all but first and last — Range

^n compiles to System.Index.FromEnd(n). A range with a ^ bound compiles to a System.Range and follows its rules: the end is exclusive whether it is written with .. or ..., so array[1..^1] leaves out the last element.

  • Element types other than Int (Long, Float, Char)
  • How a range converts to System.Range when passed to a .NET method or subscript

Character ranges ('a'..'z') are valid only in for and case/when contexts. They are not first-class Range values and cannot be assigned to a variable:

for char in 'a'..'z' # ok
puts char
end
case letter
when 'a'..'m' then "first half"
when 'n'..'z' then "second half"
end
r = 'a'..'z' # compile error — character ranges are not Range values

Tuples are fixed-length, heterogeneous sequences. The literal syntax is parenthesized, comma-separated values — consistent with C# and Python:

pair = ("Alice", 30) # Tuple[String, Int]
triple = ("Alice", 30, :admin) # Tuple[String, Int, Symbol]

Tuples map to .NET ValueTuple and are value types.

A trailing comma disambiguates a single-element tuple from a parenthesised expression:

single = (42,) # Tuple[Int]
plain = (42) # Int — parenthesised expression, not a tuple

Elements are accessed by zero-based index using dot notation:

pair = ("Alice", 30)
pair.0 # "Alice" : String
pair.1 # 30 : Int

The compiler resolves the type of each element statically from its position.

Tuples are the natural return type for methods that produce multiple values, and support destructuring directly:

def min_max(items: Array[Int]): Tuple[Int, Int]
(items.min, items.max)
end
low, high = min_max(scores)

See Destructuring for full syntax.