Joyeer Language Specification
Chapters
On this page (6 sections)

§9 Contracts & Runtime Checks

Joyeer uses runtime checks instead of declaration-level contract keywords. There is no requires / ensures / invariant syntax in v0.1.

9.1 Contract style in Joyeer

Contract intent is expressed in executable code:

func divide(a: Int, b: Int): Int {
  precondition(condition: b != 0, message: "divide: denominator must be non-zero")
  return a / b
}

9.2 Standard checks

func assert(condition: Bool, message: String): Void
func precondition(condition: Bool, message: String): Void
func fatalError(message: String): Never

Compiler-generated arithmetic and bounds checks are separate. Optimizations may remove a redundant check only when observable behavior is preserved.

9.3 Optional-first contract discipline

Uncertainty is modeled with T? (Optional), not with nullable-by-default references. Code should unwrap explicitly and fail explicitly when required.

func requirePort(parsed: Int?): Int {
  let v = match parsed {
    .Some(port) => port,
    .None => fatalError(message: "invalid port"),
  }
  precondition(condition: v >= 0 && v <= 65535, message: "port out of range")
  return v
}

Postfix force unwrap (x!) remains deferred. Use an explicit match, or ? in a function that can propagate the absence.

9.4 Writing postconditions in code

Postconditions are expressed with local snapshots and assert:

func bumpAndGet(x: inout Int): Int {
  let old = x
  &x = x + 1
  assert(condition: x == old + 1, message: "bump must advance by one")
  return x
}

9.5 Compiler treatment

9.6 Verification boundary

Design rationale. Runtime checks test conditions on an execution; compile-time checking proves only the properties covered by its rules. Neither establishes arbitrary program correctness.

Keep three possible layers distinct:

Refinement types and solver integration remain future design work, not capabilities implied by the contract APIs. Any future proof system must define its supported logic and treatment of unproved properties; a timeout or failed proof must not be reported as successful verification. Automatic proof of arbitrary properties is not a release promise.