Skip to content

Basic Operators ​

Implementation status: Implemented (arithmetic / comparison / logical / assignment / range / nil coalescing / force unwrap / identity subset); custom operator func is covered in Chapter 30, Advanced Operators

SYX operators fall mainly into four categories: arithmetic, comparison, logical, assignment. They are implemented underneath through Core Host intrinsic instructions and are broadly consistent with Swift; nothing relies on runtime interpretation.

Arithmetic Operators ​

swift
let a = 10 + 3      // 13
let b = 10 - 3      // 7
let c = 10 * 3      // 30
let d = 10 / 3      // 3 (integer division)
let m = 10 % 3      // 1 (modulo; sign follows the left operand)
let n = -a           // Unary minus
OperatorMeaningApplicable types
+ - * / %Add / subtract / multiply / divide / moduloIntegers, floats
-xUnary minusNumeric
+xUnary plus (syntactically allowed)Numeric

Overflow: integer arithmetic on the VM / MIR / WASM paths and fixed-width operations always wrap two's complement (wrapping_* is the floor; independent of debug/release). The debug R011 panic once described in this book was not delivered. Swift-style wrapping operators &+ / &- / &* are not implemented. Unary -Int.min does not trap (wraps; not overflow-trap GA — aligned with gate A-04). Stage-1 charter: S1-07 scoped wrap SSOT (≠ trap GA). Check bounds explicitly when writing loops/bit operations, or move to a wider type.

Division by zero: integer /0 or %0 triggers R012 division by zero; float division by zero yields inf / nan.

Mixed types: Int + Double follows the widening rule (widen_for_binary). Same-width integers (e.g. Int8 + Int8) keep that width; Int8 + Int is promoted to platform Int. Int + UInt triggers S014 (avoiding a silent sign-bit wrap). Scalar bit operations & | ^ << >> ~ follow the same kind (Int8 << 1 wraps at 8 bits; Int & UInt → S014). Ordered comparisons < <= > >= follow the same kind (UInt8 compares as unsigned; ==/!= still use value equality). Unary - / ~ follow the operand's kind (-(-128 as Int8) wraps at 8 bits and stays -128).

Comparison Operators ​

swift
let x = 1
let y = 2
print(x == y)      // Equal → false
print(x != y)      // Not equal → true
print(x < y)       // Less than → true
print(x <= y)      // Less than or equal → true
print(x > y)       // Greater than → false
print(x >= y)      // Greater than or equal → false
FormApplies to
== / !=Value equality (Equatable protocol witness; custom types need data class + @Equatable)
=== / !==Identity (reference types compare references / metatype tags; see Chapter 30, Advanced Operators)
< <= > >=Ordered comparison (Comparable protocol witness; data class + L1 synthesis or custom)

Not comparable: using < and friends on a type without Comparable conformance → S004 type mismatch.

Logical Operators ​

swift
let a = true
let b = false
let flag = false
if (a && b) { print("and") }    // Short-circuit AND
if (a || b) { print("or") }     // Short-circuit OR
if (!flag) { print("not") }     // Logical NOT
OperatorMeaningShort-circuits
&&Logical AND✅ The right side is evaluated only when the left is true
||Logical OR✅ The right side is evaluated only when the left is false
!Logical NOTUnary

Operands must be Bool; a non-Bool expression condition → S004.

Nil Coalescing and Optional Chaining ​

swift
let optional: Int? = nil
let defaultValue = 0
let maybeString: String? = "hi"
let v = optional ?? defaultValue     // Nil coalescing → 0
let len = maybeString?.count        // Optional chaining
let forced = maybeString!            // Force unwrap → "hi"
OperatorMeaningFailure behavior
??Nil coalescingTakes the right side when the left is nil
?.Optional chainingAny nil link makes the whole thing nil
!Force unwrapnil → runtime error R007
swift
let s: String? = "hello"
print(s?.count)        // Optional(5)  ← Note: still an Optional
let n: Int = s?.count ?? 0   // 5
let bad: Int = s!.count      // 5 (force unwrap succeeds)

Assignment and Compound Assignment ​

swift
var x = 1          // Simple assignment
x += 2             // Equivalent to x = x + 2
x -= 1
x *= 2
x /= 2
x %= 3
var arr = [0, 1, 2]
arr[1] = 9
FormMeaningFailure
=Simple assignmentOn a let → S003
+= -= *= /= %=Compound assignmentSame as above
obj.field = vMember assignmentRead-only field or immutable object → S003
arr[i] = vSubscript assignmentOut of bounds → runtime R003

Assignment expressions: in SYX assignment does not return a Void value usable for chaining (consistent with Swift). a = b = c is compile error S004.

Fixed-width compound assignment: for fixed-width integers such as Int8, +=/-=/*=//=/%=/&=/|=/^=/<<=/>>= desugar to binary operations carrying the widen_for_binary kind (e.g. 120i8 += 10 → -126; -1i8 &= 15 → 15; 1i8 |= 15 → 15; 15i8 ^= 1 → 14; 64i8 <<= 1 → -128; -128i8 >>= 1 → -64). For platform Int, +=/-= may take a host fast path. The bitwise compound grammar (including shift compounds) is complete.

Range Operators ​

swift
for i in 0..<10 { print(i) }   // Half-open [0, 10)
for j in 1...5 { print(j) }    // Closed [1, 5] (integer literal endpoints)
FormMeaningStatus
a..<bHalf-open [a, b)Implemented
a...bClosed [a, b] (integer literal endpoints)MVP GA (Phase 139)
a..<Unbounded on the rightNot implemented
..<bUnbounded on the leftNot implemented

The type of a range expression is Range<Int> (in for-in and contains contexts).

swift
print((1..<10).contains(5))    // true
print((1..<10).contains(10))   // false
print((1...10).contains(10))  // true
print((1...10).contains(11))   // false

Unary Prefix Operators ​

OperatorMeaningApplies to
-Unary minusNumeric
!Logical NOT / force unwrapBool / Optional
tryError propagation / ? chainsthrows calls
awaitAsynchronous waitasync context
&Borrow expression&T / &mut T

The Ternary (Conditional) Operator ​

SYX does not provide the ?: ternary operator directly; use an if expression or a temporary variable instead:

swift
let x = 1
// ✅ Recommended
let label = if (x > 0) { "pos" } else { "non-pos" }

// ❌ Not supported
// let label = x > 0 ? "pos" : "non-pos"   // Compile error

Why no ?:: consistent with Swift; the goal is to encourage readability, since nested ternaries are hard to read.

Operator Precedence ​

For the complete precedence table, see Language Reference: Expressions. Summary, from highest to lowest:

LevelOperators
1 (highest). [] () ?. ! await (postfix)
2- ! try (unary prefix)
3* / %
4+ -
5..< ...
6< <= > >=
7== != === !==
8&&
9||
10??
11 (lowest)= += -= *= /= %=

Parentheses win: () may be added anywhere to force precedence, with no side effects.

Numeric Promotion and Literal Rules ​

swift
let a: Int = 1
let b = a + 1.0   // Double (`1.0` is a Double literal; `a` is promoted to Double)
let c = a + 1     // Int (`1` is an Int literal)
let d = 1_000     // Int (underscores for readability)
LiteralInferred type
42Int
42i8Int8
3.14Double
3.14f32Float32
"text"String
trueBool
nilContextually inferred (usually Optional<T>)

Example: Accumulating ​

swift
func main() {
    var sum = 0
    for i in 0..<5 {
        sum += i        // 0+1+2+3+4 = 10
    }
    print(sum)          // 10
}

Custom Operators ​

operator func lets you declare new operators (Chapter 30):

swift
// Custom infix `|>` lives in Ch30; here a same-file function stand-in:
func pipe(value: Int, f: (Int) -> Int) -> Int { return f(value) }
let result = pipe(value: 5, f: { $0 * 2 })    // 10

For the complete grammar, precedence groups, and custom infix / prefix / postfix operators, see Chapter 30, Advanced Operators.

Diagnostics ​

CodeTriggerFix
S003Assignment to letChange it to var
S004Incompatible operand typesAdd an explicit conversion / change the type
S008Narrowing (Int64 → Int8)Explicit truncation / range check
S014Mixed signed/unsignedExplicit cast
R007! unwrapping nilUse ?. / ?? / if let
R011Integer overflow Display (not delivered; the current behavior is wrapping)Check bounds; you can move to a wider type; the same code may be reused by other domains (e.g. the HTTP client)
R012Integer division by zeroCheck the divisor
R003Array / string subscript out of boundsExplicit range check

For complete diagnostics, see Appendix B: Diagnostic Code Index.

Differences from Swift ​

PointSYX
===Supported, with a reference/type-sensitive subset
Custom operatorsGA (operator func + three-back-end parity)
OverflowAlways two's-complement wrapping (≠ Swift default trap; R011 not delivered; &+/&-/&* not implemented; -Int.min wraps, not trap GA)

Next chapter → Strings and Characters

See also → Language Reference: Expressions · Chapter 30, Advanced Operators · Chapter 32, Operators vs Core Host

SYX · Apache-2.0