sapphire-lang 0.8.0

Gradually typed scripting language where every value is an object and types are optional, checked at runtime
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
# Sapphire Tutorial

Sapphire is an object-oriented scripting language with a Ruby-inspired feel: clean syntax, everything is an object, and blocks make iteration expressive. This tutorial walks through the language from the ground up.

## Running Sapphire

```sh
sapphire run hello.spr   # run a script file
sapphire console         # start the interactive REPL
```

File extension: `.spr`

---

## The basics

Variables are assigned with `=`. No declaration keyword is needed. String literals use double quotes, and you can embed any expression inside `#{}`:

```ruby
name = "Alice"
age = 30
pi = 3.14

print "Hello, #{name}!"           # Hello, Alice!
print "Next year: #{age + 1}"     # Next year: 31
print "Pi is about #{pi}"         # Pi is about 3.14
```

Comments begin with `#` and run to the end of the line. The `print` built-in accepts any value and writes it to standard output.

Sapphire has six built-in value types:

| Type   | Examples                          |
|--------|-----------------------------------|
| Int    | `0`, `42`, `-7`                   |
| Float  | `3.14`, `-0.5`                    |
| Bool   | `true`, `false`                   |
| Str    | `"hello"`                         |
| List   | `[1, 2, 3]`                       |
| Map    | `{ name: "Alice", age: 30 }`      |
| Nil    | `nil`                             |

Variable names contain letters, digits, and underscores. The `?` suffix is reserved for method names — not variables.

---

## Control flow

### if / elsif / else

```ruby
score = 72

if score >= 90 {
  print "A"
} elsif score >= 80 {
  print "B"
} elsif score >= 70 {
  print "C"
} else {
  print "below C"
}
# C
```

A trailing `if` is a concise way to guard a single statement:

```ruby
print "passing" if score >= 60
```

### while, break, and next

```ruby
i = 1
sum = 0

while i <= 10 {
  next if i % 2 == 0   # skip even numbers
  sum = sum + i
  break if sum > 15    # stop once we've accumulated enough
  i = i + 1
}

print sum   # 16  (1+3+5+7)
```

`next` skips to the next iteration; `break` exits the loop entirely.

### Ranges

A range literal `from..to` represents an inclusive sequence of integers. Use `.each` to iterate or `.include?` to test membership:

```ruby
(1..5).each { |i| print i }
# 1  2  3  4  5

(1..100).include?(42)   # true
```

---

## Functions

Define functions with `def`. The last evaluated expression is the implicit return value — no explicit `return` needed except for early exits.

```ruby
def greet(name) {
  "Hello, #{name}!"
}

print greet("world")   # Hello, world!
```

### Gradual typing

Type annotations on parameters and return values are optional. When present, they are enforced at runtime — that's gradual typing. Add them where you want safety; leave them off when flexibility matters more.

```ruby
# Without annotations — works for any value
def double(x) {
  x * 2
}

# With annotations — enforced at runtime
def double(x: Int) -> Int {
  x * 2
}

print double(5)     # 10
print double(2.5)   # runtime error: expected Int
```

Annotate as much or as little as you like. A common pattern is to annotate public functions at module boundaries and leave internal helpers unannotated.

### Early return

`return` is useful for exiting a function before reaching the end:

```ruby
def abs(n: Int) -> Int {
  return -n if n < 0
  n
}
```

### Predicates

By convention, functions that return a boolean end with `?`:

```ruby
def even?(n: Int) -> Bool {
  n % 2 == 0
}

print even?(4)   # true
print even?(7)   # false
```

### Closures

Functions close over variables in scope where they are defined:

```ruby
def make_adder(n) {
  def adder(x) {
    x + n
  }
  adder
}

add5 = make_adder(5)
print add5(3)   # 8
print add5(10)  # 15
```

### yield

`yield` calls the block passed to the current method, letting you write your own iterators:

```ruby
def repeat(n: Int) {
  i = 0
  while i < n {
    yield(i)
    i = i + 1
  }
}

repeat(3) { |i| print "step #{i}" }
# step 0
# step 1
# step 2
```

---

## Collections

### Lists

List literals use square brackets. Indexing is zero-based; negative indices count from the end.

```ruby
nums = [3, 1, 4, 1, 5, 9, 2, 6]

print nums[0]    # 3
print nums[-1]   # 6
print nums.size   # 8

nums.append(7)
nums.pop()       # remove and return last
```

The core iteration methods let you work with lists without manual loops:

```ruby
nums = [3, 1, 4, 1, 5, 9, 2, 6]

doubled  = nums.map    { |n| n * 2 }
big      = nums.select { |n| n > 4 }
total    = nums.reduce(0) { |acc, n| acc + n }

print doubled   # [6, 2, 8, 2, 10, 18, 4, 12]
print big       # [5, 9, 6]
print total     # 31

print nums.any? { |n| n > 8 }   # true   (9 is)
print nums.all? { |n| n > 0 }   # true
print nums.none? { |n| n > 99 } # true
```

Blocks can also read and write outer variables:

```ruby
sum = 0
nums.each { |n| sum = sum + n }
print sum   # 31
```

### Maps

Map literals use `{ key: value }` syntax. Keys are always strings.

```ruby
person = { name: "Alice", age: 30 }

print person["name"]   # Alice

person["city"] = "Dublin"

print person.keys      # ["name", "age", "city"]
print person.size    # 3

person.each { |k, v| print "#{k}: #{v}" }
```

---

## Classes

### Fields and methods

Define a class with `class`. Use `attr` to declare fields. Instantiate with `ClassName.new(field: value, ...)`.

Inside a method body, fields and other methods are accessible by their bare name. `self.` is only required when *writing* to a field.

```ruby
class Point {
  attr x: Int
  attr y: Int

  def distance_sq() -> Int {
    x * x + y * y
  }

  def to_s() -> Str {
    "(#{x}, #{y})"
  }
}

p = Point.new(x: 3, y: 4)
print p.to_s()            # (3, 4)
print p.distance_sq()     # 25
```

Fields can have default values:

```ruby
class Circle {
  attr radius: Int
  attr color = "red"
}

c = Circle.new(radius: 5)
print c.color   # red
```

### Mutating fields

Reading uses the bare field name; writing requires `self.field =`:

```ruby
class Counter {
  attr count = 0

  def increment() {
    self.count = count + 1
  }

  def reset() {
    self.count = 0
  }
}

c = Counter.new(count: 0)
c.increment()
c.increment()
c.increment()
print c.count   # 3
c.reset()
print c.count   # 0
```

### Private methods

`defp` declares a private method — callable from within the class but not from outside:

```ruby
class BankAccount {
  attr balance = 0

  def deposit(amount: Int) {
    self.balance = balance + validate(amount)
  }

  defp validate(amount: Int) -> Int {
    raise "amount must be positive" if amount <= 0
    amount
  }
}

acc = BankAccount.new(balance: 0)
acc.deposit(100)
print acc.balance   # 100

acc.validate(50)    # error: private method
```

### Class methods

A `self { ... }` block inside the class body defines methods called on the class itself. Use them for factory methods and class-level behaviour:

```ruby
class Point {
  attr x: Int
  attr y: Int

  self {
    def origin() {
      self.new(x: 0, y: 0)
    }
  }
}

p = Point.origin()
print p.x   # 0
```

### Inheritance

Use `class Child < Parent` to inherit from a parent class. Subclasses inherit all fields and methods, and can override methods:

```ruby
class Animal {
  attr name: Str

  def speak() {
    print "..."
  }

  def greet() {
    print "I am #{name}"
  }
}

class Dog < Animal {
  def speak() {
    print "Woof!"
  }
}

class Cat < Animal {
  def speak() {
    print "Meow!"
  }
}

d = Dog.new(name: "Rex")
c = Cat.new(name: "Whiskers")

d.speak()   # Woof!
d.greet()   # I am Rex  (inherited from Animal)
c.speak()   # Meow!
```

Use bare `super` or `super(...)` to call the superclass method with the same name (like Ruby):

```ruby
class Animal {
  attr name: Str

  def describe() -> Str {
    name
  }
}

class Dog < Animal {
  attr breed: Str

  def describe() -> Str {
    super() + " (#{breed})"
  }
}

d = Dog.new(name: "Rex", breed: "Lab")
print d.describe()   # Rex (Lab)
```

Every class implicitly inherits from `Object`. `is_a?(ClassName)` checks the class hierarchy:

```ruby
d = Dog.new(name: "Rex", breed: "Lab")
d.is_a?(Dog)      # true
d.is_a?(Animal)   # true
d.is_a?(Object)   # true
d.is_a?(Cat)      # false
```

---

## Error handling

Use `raise` to signal an error. Handle errors with a `begin / rescue / end` block:

```ruby
def parse_age(s: Str) -> Int {
  age = s.to_i
  raise "age must be positive" if age <= 0
  age
}

begin
  print parse_age("25")   # 25
  print parse_age("0")    # raises
rescue e
  print "bad input: #{e}"
end
```

The `else` clause runs only when no error occurred:

```ruby
begin
  result = 10 / 2
rescue e
  print "error: #{e}"
else
  print "result: #{result}"   # result: 5
end
```

A `rescue` clause inside a `def` body avoids the `begin / end` wrapper entirely:

```ruby
def safe_divide(a: Int, b: Int) -> Int {
  a / b
rescue e
  print "cannot divide by zero"
  0
}

print safe_divide(10, 2)   # 5
print safe_divide(10, 0)   # 0
```

You can raise any object, not just strings:

```ruby
class AppError {
  attr message: Str
}

begin
  raise AppError.new(message: "not found")
rescue e
  print e.message   # not found
end
```

---

## Imports

Split your code across files with `import`. The path must be relative (starting with `./` or `../`) and the `.spr` extension is added automatically:

```ruby
import "./utils"
import "../shared/helpers"
```

Everything defined at the top level of the imported file — classes and functions — becomes available in the importing file. Each file is only executed once, even if imported from multiple places.

A common pattern is a library file that defines a class:

```ruby
# geometry/point.spr
class Point {
  attr x: Float
  attr y: Float

  def distance_to(other) -> Float {
    dx = self.x - other.x
    dy = self.y - other.y
    ((dx * dx) + (dy * dy)).sqrt()
  }

  def to_s() -> Str {
    "(#{self.x}, #{self.y})"
  }
}
```

And a main file that uses it:

```ruby
# main.spr
import "./geometry/point"

a = Point.new(x: 0.0, y: 0.0)
b = Point.new(x: 3.0, y: 4.0)

print a.to_s()              # (0.0, 4.0)
print a.distance_to(b)      # 5.0
```

Files can import other files — a file inside `geometry/` can itself import from the same directory using `./`:

```ruby
# geometry/shapes.spr
import "./point"

class Circle {
  attr center: Point
  attr radius: Float

  def area() -> Float {
    3.14159 * self.radius * self.radius
  }
}
```

---

## Putting it together

Here is a small program that uses classes, inheritance, type annotations, blocks, and error handling. It models geometric shapes and computes statistics over a collection of them.

```ruby
class Shape {
  def area() -> Float {
    raise "area() not implemented"
  }

  def describe() -> Str {
    "Shape with area #{self.area()}"
  }
}

class Rectangle < Shape {
  attr width: Float
  attr height: Float

  def area() -> Float {
    width * height
  }

  def describe() -> Str {
    "Rectangle #{width}x#{height}, area=#{self.area()}"
  }
}

class Circle < Shape {
  attr radius: Float

  def area() -> Float {
    3.14159 * radius * radius
  }

  def describe() -> Str {
    "Circle r=#{radius}, area=#{self.area()}"
  }
}

def summarise(shapes) {
  shapes.each { |s| print s.describe() }

  total = shapes.reduce(0.0) { |acc, s| acc + s.area() }
  largest = shapes.reduce(shapes[0]) { |best, s|
    if s.area() > best.area() { s } else { best }
  }

  print "Total area:   #{total}"
  print "Largest:      #{largest.describe()}"
}

shapes = [
  Rectangle.new(width: 4.0, height: 5.0),
  Circle.new(radius: 3.0),
  Rectangle.new(width: 2.0, height: 8.0),
]

begin
  summarise(shapes)
rescue e
  print "Error: #{e}"
end
```

Output:

```
Rectangle 4.0x5.0, area=20.0
Circle r=3.0, area=28.27431
Rectangle 2.0x8.0, area=16.0
Total area:   64.27431
Largest:      Circle r=3.0, area=28.27431
```