Null Safety in SP

In SP, standard types cannot contain null unless explicitly declared as nullable with a question mark (T?). This eliminates billion-dollar null pointer exceptions at compile time.

null_safety.sp
1import std.io;
2
3class Profile {
4 public string bio;
5 public Profile(string bio) {
6 this.bio = bio;
7 }
8}
9
10class User {
11 public Profile? profile; // Nullable type
12 public User(Profile? p) {
13 this.profile = p;
14 }
15}
16
17fn main() -> void {
18 User u1 = new User(null);
19 User u2 = new User(new Profile("Systems Architect"));
20
21 // 1. Safe Navigation Operator (?.)
22 string? bio1 = u1.profile?.bio;
23 string? bio2 = u2.profile?.bio;
24
25 // 2. Null Coalescing Operator (??)
26 string displayBio = bio1 ?? "No biography provided";
27
28 println("User 1 Bio: ${displayBio}");
29 println("User 2 Bio: ${bio2 ?? 'Default'}");
30}

Safe Operators

Safe Navigation: a?.b

If a is null, the expression short-circuits and evaluates to null without crashing.

Null Coalescing: a ?? b

If a is not null, yields a; otherwise evaluates and returns fallback default b.