Literals
Strings
Section titled “Strings”Strings are immutable by default, mapping to System.String. StringBuilder
(System.Text.StringBuilder) is available via normal .NET interop with no special
syntax.
Double-quoted strings
Section titled “Double-quoted strings”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"Character literals
Section titled “Character literals”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 StringThere are no single-quoted strings. For a string without interpolation or escape
processing, use a raw string or a <<~'MSG' heredoc.
Raw strings
Section titled “Raw strings”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 }"""Heredocs
Section titled “Heredocs”Ruby-style squiggly heredoc — strips leading indentation to match the least-indented line. Supports interpolation:
message = <<~MSG Hello, #{name}! Your score is #{score}.MSGNo-interpolation heredoc:
template = <<~'MSG' No #{interpolation} here.MSGEscape Sequences
Section titled “Escape Sequences”| 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
Section titled “Regex Literals”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/iRegex 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 newlineMatching
Section titled “Matching”match = /\d+/.match("abc123") # Match? — nil if no matchmatch = "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 matchm[1] # "2024" — first capture groupm[2] # "03" — second capture groupm[:year] # named capture group (?<year>\d+)m.start # start index in original stringm.end # end indexCapture 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.
Replacement
Section titled “Replacement”"hello world".gsub(/\w+/) { |m| m.upcase } # "HELLO WORLD""hello world".sub(/\w+/, "goodbye") # "goodbye world""one two three".split(/\s+/) # ["one", "two", "three"]Symbols
Section titled “Symbols”Lightweight interned identifiers, backed by Amethyst.Runtime.Symbol:
:name:user_id:"complex symbol with spaces"Array Literals
Section titled “Array Literals”[1, 2, 3] # Array[Int]["alice", "bob"] # Array[String][1, 2.5] # Array[Float] — Int widened to FloatThe 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 typeitems: Array[Any] = [1, "one"] # okHash Literals
Section titled “Hash Literals”{ name: "Alice", role: "admin" } # Hash[Symbol, String] — `key:` makes a symbol key{ "alice" => 1, "bob" => 2 } # Hash[String, Int] — `=>` takes a key of any typeKeys 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 }).
Empty Collections
Section titled “Empty Collections”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 contextnames: Array[String] = []
# Explicit — both forms are equivalentnames = Array[String].newnames = [] of StringThe of suffix is also valid on hash literals:
lookup = {} of Symbol => StringArray and Symbol List Literals
Section titled “Array and Symbol List Literals”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]Numeric Literals
Section titled “Numeric Literals”42 # Int42_000_000 # Int with separators3.14 # Float0xFF # Hex Int0b1010 # Binary Int0o777 # Octal IntSuffixes
Section titled “Suffixes”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.
Range Literals
Section titled “Range Literals”1..10 # inclusive range — Range1...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..10r.start # 1r.end # 10 (included)Ranges support array and string slicing using the standard subscript syntax:
array[2..5] # elements at indices 2, 3, 4, 5string[0...3] # first three charactersRanges are enumerable — each, map, to_array, etc. are available via the
Enumerable mixin.
End-Relative Indices
Section titled “End-Relative Indices”.NET-style hat (^) indices address from the end of a collection. These are valid
in range literals and subscripts:
array[^1] # last element — System.Indexarray[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.
Open Questions [TBD]
Section titled “Open Questions [TBD]”- Element types other than
Int(Long,Float,Char) - How a range converts to
System.Rangewhen passed to a .NET method or subscript
Character Ranges
Section titled “Character Ranges”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 charend
case letterwhen 'a'..'m' then "first half"when 'n'..'z' then "second half"end
r = 'a'..'z' # compile error — character ranges are not Range valuesTuple Literals
Section titled “Tuple Literals”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.
Single-Element Tuples
Section titled “Single-Element Tuples”A trailing comma disambiguates a single-element tuple from a parenthesised expression:
single = (42,) # Tuple[Int]plain = (42) # Int — parenthesised expression, not a tupleAccessing Elements
Section titled “Accessing Elements”Elements are accessed by zero-based index using dot notation:
pair = ("Alice", 30)pair.0 # "Alice" : Stringpair.1 # 30 : IntThe compiler resolves the type of each element statically from its position.
Destructuring
Section titled “Destructuring”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.