Skip to content

Nullability and Type Checking

Kelvra is strictly typed by default. The repo has broad type-error coverage, and the docs should reflect that posture instead of softening it.

Nullable types are written with ?.

var maybeScore i32? = 7
if (maybeScore != null) {
print("score present")
}

You can also assign null later:

type Dog struct {
fn bark() str {
return "woof"
}
}
var maybeDog Dog? = Dog()
maybeDog = null

The current type checker expects explicit checks before optional member access or optional calls. The repo includes negative tests for both.

if (maybeDog != null) {
print("dog present")
}

The type checker already rejects:

  • assigning null to non-optional bindings
  • assigning the wrong primitive or collection type
  • assigning to const
  • invalid subtype assignments
  • invalid handle/package-type mixing

Functions are checked for:

  • argument type mismatches
  • lambda parameter and return mismatches
  • invalid casts
  • wrong return types
  • missing values on non-void returns

The repo also enforces loop-control correctness:

  • break outside loops is rejected
  • continue outside loops is rejected
  • unknown loop labels are rejected
  • duplicate loop labels are rejected

Typed imports are checked too. Existing tests cover:

  • missing imported symbols
  • typed import binding mismatches
  • native package binding mismatches
  • import cycles surfaced during frontend analysis