Basics
Core syntax and expressions
This page covers core syntax and expression rules to help you read and write simple Nanyx code.
Expressions
Everything is an expression that produces a value.
def message = "Hello world"
Definitions
Use def to bind a name to a value. The value can be any expression, including another definition or a function:
def name = "Nanyx"
def version = 4
def getMessage = { name -> "Hello, {name}!" }
Blocks and indentation
Blocks use indentation. A more-indented line belongs to the previous line. If a line ends with ->, the next indented block is part of the expression.
def message =
def preamble = "Hello"
def audience = "World"
"{preamble} {audience}"
You can also continue an expression on the next line when the previous line is incomplete:
def message = "Hello " +
"World"
A block returns the value of its last expression:
def answer =
def x = 20
def y = 22
x + y
For more on expression forms, see Functions and Pattern matching.
Numbers
Nanyx includes familiar numeric literals:
def count = 42
def ratio = 3.14159
def million = 1_000_000
You can use standard arithmetic and comparison operators:
def total = 10 + 5 * 2
def bigger = total > 12
For operator behavior, see Operators. For range syntax, see Ranges.
Strings
There are two kinds of string literal. Double-quoted strings support interpolation with {...}:
def name = "Alex"
def greeting = "Hello, {name}!"
Single-quoted strings are raw: nothing inside them is interpolated or escaped, so braces, backslashes and quotes are taken literally. Use them for JSON, regular expressions, templates, and anything else that contains braces of its own:
def json = '{"name": "Alice", "age": 30}'
def emailPattern = '[^@]+@\w+\.\w+'
To write a literal brace inside a double-quoted string, double it:
def template = "Hello, {name}! Braces look like {{this}}."
For library string operations (length, split, trim, etc.), see string stdlib.
Equality and comparison
Use == and != for equality and inequality:
def same = 10 == 10
def different = "a" != "b"
Use <, <=, >, >= for ordering comparisons:
def isAdult = 21 >= 18
Boolean values are combined with the keywords and, or and not:
def canVote = isAdult and isRegistered
def needsHelp = not isAdult or isNew
These operators are part of the language’s operator system; see Operators for details.
Mutability
Mutability is available in Nanyx, but is considered a somewhat advanced feature. Read about the Memory context for more.
For immutable record updates, see Copy and update (non-destructive mutation).
Commas and newlines
You can use commas or newlines to separate items in lists and records:
def items = [1, 2, 3]
def items = [
1
2
3
]
def myRecord = (a = 1, b = 2, c = 3)
def myRecord = (
a = 1
b = 2
c = 3
)
Comments
Single-line comments use --. Multi-line comments start and end with a line containing only three or more ---.
-- This is a single-line comment
---
This is a multi-line comment.
---
----------------------------------
This is also a multi-line comment.
----------------------------------
Multi-line comments must be closed with the same number of dashes that they were opened with.
Tags, ranges and records
Nanyx also supports tags, ranges, and records as core value forms. See Tags, Ranges, and Records and tuples.
Table literals
Table literals are a shorthand for lists of records with the same shape:
def namesAndAges =
[| name | age |
| "Alex" | 22 |
| "Taylor" | 30 |
| "Sam" | 28 |
| "Morgan" | 45 |]
Conditionals
if is an expression with a then branch and an else branch:
def label = if count == 0 then "empty" else "has items"
if cond then
someCode()
else
someOtherCode()
When there are more than two cases, use the multi-branch form. This is sugar for a match where each arm is a boolean condition, tested in order, with else as the final catch-all:
if | cond1 ->
someCode()
| cond2 ->
someOtherCode()
| else ->
yetMoreCode()
if can also test a pattern; see Pattern matching.
Unimplemented code
Use ??? as a placeholder to keep code compiling while you iterate:
def myFunction = { () -> ??? }