Skip to content

The Basics ​

Implementation status: Implemented (including Int128 / UInt128 Phase 38, platform Int/UInt, type nesting MVP, typealias GA Phase 40)

This chapter covers constants, variables, type annotations, comments, numeric literals and type inference. Mastering these is a prerequisite for writing any SYX program.

Variables and Constants ​

swift
var mutable = 10
mutable = 20          // ✅

let immutable = 42
// immutable = 43     // ❌ Compile error S003 (cannot assign to let binding)
KeywordMeaningReassignmentRedeclaration in the same scope
letImmutable binding❌ S003❌ S001
varMutable binding✅❌ S001

Scope: block scope { … }; for / while / func each introduce a new scope. An inner scope may reuse an outer let/var name to "shadow" it (not recommended; it easily confuses readers).

Diagnostics: S003 (assignment to let), S001 (duplicate definition), S002 (undefined identifier).

Type Annotations ​

Explicit types let readers see at a glance "what type this is", and force a choice when inference is ambiguous:

swift
let x: Int = 10
let name: String = "SYX"
let items: Array<Int> = [1, 2, 3]
let scores: [String: Int] = ["a": 1, "b": 2]
let maybe: Int? = nil
Annotation positionSyntaxNotes
Local variablelet x: T = exprRequired explicitly when inference fails
Top-level variablevar count: Int = 0Usually has a default value
Function parameterfunc f(x: Int)Mandatory (no type inference)
Function return-> TDefaults to Void (i.e. ())
Propertyvar name: StringMust be explicitly annotated or given an initial value

Type inference: after its first assignment, the type of a let binding is frozen. A later incompatible assignment → S004.

swift
let n = 10       // n: Int
// n = "hello"   // ❌ S004: cannot assign String to Int

Comments ​

swift
// Single-line comment

/* Block comment
   /* may nest */
   any line
*/

/// Documentation comment (extracted into API docs by `syx doc`)
/// Multiple lines: each starting with `///`
func add(a: Int, b: Int) -> Int { return a + b }
FormUse
//Single line (to end of line or whole line)
/* … */Block comment; nesting supported (same as Rust)
///Documentation comment; syx doc . generates docs/api.md

Numeric Types and Literals ​

SYX offers both platform word size and fixed width integers, plus 128-bit and SIMD vectors (see Chapter 31, SIMD).

TypeSizeRange / notes
Int88-bit signed-128 … 127
Int1616-bit signed-32,768 … 32,767
Int3232-bit signed-2³¹ … 2³¹-1
Int6464-bit signed-2⁶³ … 2⁶³-1
Int128128-bit signedPhase 38; literal suffix i128
IntPlatform word size64-bit host → Int64; 32-bit host → Int32
UInt8 / UInt16 / UInt32 / UInt64Fixed-width unsigned
UInt128128-bit unsignedPhase 38; literal suffix u128
UIntPlatform word size, unsigned64-bit host → UInt64
Float32 / Float32-bit floating point (IEEE 754)
Float64 / Double64-bit floating pointUnsuffixed floats default to Double

Literal syntax:

swift
let a = 42         // Int (platform word size)
let b = 100i8      // Int8
let c = 1000i64    // Int64
let d = 0xFFu8     // UInt8, hexadecimal
let e = 0b1010u16  // UInt16, binary
let f = 0o17u32    // UInt32, octal
let g = 1_000_000i64  // Underscore separators (readability, removed at compile time)
let h = 42i128     // Int128
let i = 3.14f32    // Float32
let j = 3.14       // Double (default)
let k = -2.5e10    // Double, scientific notation

Underscores (_) may separate digits anywhere in a numeric literal and are ignored at compile time (e.g. 1_000_000 = 1000000).

Suffix case: i8/I8, u8/U8, f32/F32 and so on are all accepted; lowercase is recommended for consistency with the Swift style.

Signed vs. unsigned: mixed arithmetic may trigger S014 (avoiding silent wrapping). For explicit conversion, see Chapter 20, Type Casting.

Booleans and Nil ​

swift
let ok: Bool = true
let ng: Bool = false
let empty: String? = nil
let present: String? = "hi"
LiteralType
true / falseBool
nilAny T? (nil literal, type inferred from context)

Type Aliases (typealias) ​

Give a short name to a long type (Phase 40 GA):

swift
typealias Handle = UInt64
typealias Handler = (Int) -> String

func register(h: Handle) {}
let f: Handler = { n in "id=\(n)" }
  • typealias does not create a new type; it is only a synonym
  • No where clause is supported; generic parameters must be copied 1:1
  • Equivalent to Swift's typealias, but it cannot replace associatedtype

Diagnostic: a cyclic definition → S004 (type recursion detected)

Type Nesting ​

Nesting sub-types inside a type is a core Swift pattern. SYX supports it in a limited way:

swift
class Outer {
    var name: String
    init(name: String) { self.name = name }

    class Inner {           // ⚠️ Partially implemented
        var value: Int = 0
    }
}

let o = Outer(name: "x")
let i = Outer.Inner()      // Flattening may be needed (see below)

Current status:

  • Top-level class / struct / enum: Implemented
  • Declaring another type inside a type: Partially implemented (limited parser capability)
  • Outside visibility of nested types: incomplete

Alternative (recommended, consistent with the Swift module style):

swift
struct OuterMarker { }
struct OuterMarker_Inner {
    value: Int
}

See Chapter 21, Nested Types.

nil and a Preview of Optionals ​

swift
let empty: String? = nil
let present: String? = "hi"
let length: Int? = present?.count    // 4
let safe: Int = empty?.count ?? 0   // 0 (nil coalescing)
let crash: Int = present!.count     // 4 (force unwrap)

Three ways to unwrap:

FormOn failureBest for
?. optional chainingThe whole expression is nilMulti-level access, avoiding nested ifs
! force unwrapRuntime error R007When you are 100% sure it is non-nil
?? nil coalescingFalls back to the defaultProviding a fallback

See Chapter 30, Advanced Operators for complete usage.

Int(String) → Int? (R-38 output pin · SEM-INTSTR) ​

Failable conversion prints the platform Int on success and nil on failure:

swift,output
// prints: 42\n43\nnil
func main() {
    print(Int("42"))
    print(Int("42") + 1)
    let x: Int? = Int("abc")
    print(x)
}

Naming Conventions ​

Community conventions in SYX (not enforced; syx lint reports L001–L012 warnings):

CategoryNamingExample
TypesUpperCamelCaseGreeter, MyType
Functions / variables / propertieslowerCamelCasegreet, firstName
ConstantslowerCamelCasedefaultPort (not ALL CAPS)
Enum caseslowerCamelCasecase success, case failure
ProtocolsUpperCamelCase (often ending in able/ible/Convertible)Iterator, CustomStringConvertible
Fileskebab-case.sy or snake_case.symain.sy, local_math.sy

Differences from Swift ​

PointSYX
Int widthPlatform word size (64-bit host → Int64); Swift fixes it at 64 bits on Apple platforms
Int128 / UInt128Phase 38 GA (Swift still has no native 128-bit integers)
typealiasImplemented (Phase 40 GA)
Multiline strings """MVP GA (Phase 139); may contain raw newlines and \(expr) interpolation — not the full Swift raw/#""" delimiter set
Tuple syntaxMVP GA (Phase 139): (T, U) types and literals, named elements (x: 1, y: 2), let (x, y) = t destructuring, .0/.x members — not arbitrary arity protocols / the full Swift tuple feature set
Underscores in numeric literals✅ Supported (same as Swift)
0x / 0o / 0b prefixes✅ Supported

Implementation Status and Limits ​

  • Explicit typealias: GA (Phase 40; fixtures/parity/generics.sy)
  • Int128 / UInt128: GA (Phase 38)
  • Type nesting: Partially implemented; flattening into names is recommended
  • Multiline string literals """ : MVP GA (fixtures/lang/string_multiline.sy)
  • Tuple types / literals / named elements: MVP GA (fixtures/lang/tuple.sy)
swift
let pair: (Int, String) = (1, "a")
print(pair.0)
let named = (x: 10, y: 20)
print(named.x)
let (a, b) = (3, 4)

Next chapter → Basic Operators

See also → Language Reference: Lexical Structure · Language Reference: Types · Chapter 3, Basic Operators

SYX · Apache-2.0