Skip to content

Commit 1adc931

Browse files
authored
Extend code samples to be runnable
1 parent caae6e2 commit 1adc931

File tree

133 files changed

+1244
-449
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+1244
-449
lines changed
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1-
fun test(a: Wombat iso) =>
2-
var b: Wombat tag = a // Allowed!
1+
actor Main
2+
new create(env: Env) =>
3+
test(Wombat)
4+
5+
fun test(a: Wombat iso) =>
6+
var b: Wombat tag = a // Allowed!
7+
8+
class Wombat
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1-
fun test(a: Wombat iso) =>
2-
var b: Wombat iso = a // Not allowed!
1+
actor Main
2+
new create(env: Env) =>
3+
test(Wombat)
4+
5+
fun test(a: Wombat iso) =>
6+
var b: Wombat iso = a // Not allowed!
7+
8+
class Wombat
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1-
fun test(a: Wombat trn) =>
2-
var b: Wombat box = a // Allowed!
1+
actor Main
2+
new create(env: Env) =>
3+
test(Wombat)
4+
5+
fun test(a: Wombat trn) =>
6+
var b: Wombat box = a // Allowed!
7+
8+
class Wombat

code-samples/appendices-annotations-empty-with-nosupertype-annotation.pony

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@ class Foo
44
fun foo[A: Any](a: (A | Empty val)) =>
55
match consume a
66
| let a': A => None
7-
end
7+
end
8+
9+
actor Main
10+
new create(env: Env) =>
11+
let foo: Foo = Foo
12+
env.out.print(foo.foo[Any]("Something").string())
Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
1-
if \likely\ cond then
2-
foo
3-
end
1+
type T is (U32|U8)
42

5-
while \unlikely\ cond then
6-
bar
7-
end
3+
actor Main
4+
new create(env: Env) =>
5+
let foo = "foo"
6+
let bar = "bar"
7+
let baz = "baz"
8+
var cond = true
9+
let obj: U32 = 42
10+
let expr: U32 = 42
11+
12+
if \likely\ cond then
13+
foo
14+
end
815

9-
repeat
10-
baz
11-
until \likely\ cond end
16+
cond = false
17+
while \unlikely\ cond do
18+
bar
19+
end
1220

13-
match obj
14-
| \likely\ expr => foo
15-
| \unlikely\ let capt: T => bar
16-
end
21+
cond = true
22+
repeat
23+
baz
24+
until \likely\ cond end
25+
26+
let res =
27+
match obj
28+
| \likely\ expr => foo
29+
| \unlikely\ let capt: T => bar
30+
end
31+
32+
env.out.print("res = " + res)
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
struct \packed\ MyPackedStruct
22
var x: U8
3-
var y: U32
3+
var y: U32
4+
5+
new create() =>
6+
x = 0
7+
y = 1
8+
9+
actor Main
10+
new create(env: Env) =>
11+
env.out.print("{\n\t\"x\": " + MyPackedStruct.x.string() + ",\n\t\"y\": " + MyPackedStruct.y.string() + "\n}")
Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
1-
let dice: Array[U32] = [1; 2; 3
2-
4
3-
5
4-
6
5-
]
1+
use "random"
2+
3+
actor Main
4+
new create(env: Env) =>
5+
let dice: Array[U32] = [1; 2; 3
6+
4
7+
5
8+
6
9+
]
10+
Rand.shuffle[U32](dice)
11+
for numberOfSpots in dice.values() do
12+
env.out.print("You rolled a " + _ordinal(numberOfSpots))
13+
end
14+
15+
fun _ordinal(number: U32): String =>
16+
match number
17+
| 1 => "one"
18+
| 2 => "two"
19+
| 3 => "three"
20+
| 4 => "four"
21+
| 5 => "five"
22+
| 6 => "six"
23+
else
24+
"out of range"
25+
end
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
use "format"
2+
use "collections/persistent"
3+
14
primitive Colours
25
fun black(): U32 => 0xFF000000
3-
fun red(): U32 => 0xFFFF0000
6+
fun red(): U32 => 0xFFFF0000
7+
8+
interface val Applyable
9+
fun apply(): U32
10+
11+
actor Main
12+
new create(env: Env) =>
13+
let colorMap: Map[String, Applyable] = Map[String, Applyable].concat([
14+
("red", Colours~red())
15+
("black", Colours~black())
16+
].values())
17+
18+
for (colorName, color) in colorMap.pairs() do
19+
env.out.print(colorName + ": #" + Format.int[U32](color(), FormatHexBare))
20+
end
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
1+
use "format"
2+
use "collections/persistent"
3+
14
primitive Black fun apply(): U32 => 0xFF000000
2-
primitive Red fun apply(): U32 => 0xFFFF0000
5+
primitive Red fun apply(): U32 => 0xFFFF0000
6+
7+
type Color is (Red | Black)
8+
9+
actor Main
10+
new create(env: Env) =>
11+
12+
let colorMap: Map[String, Color] = Map[String, Color].concat([
13+
("red", Red)
14+
("black", Black)
15+
].values())
16+
17+
for (colorName, color) in colorMap.pairs() do
18+
env.out.print(colorName + ": #" + Format.int[U32](color(), FormatHexBare))
19+
end
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,16 @@
1-
a
2-
-b
1+
use "format"
2+
use "collections/persistent"
3+
4+
primitive Black fun apply(): U32 => 0xFF000000
5+
primitive Red fun apply(): U32 => 0xFFFF0000
6+
7+
type Color is (Red | Black)
8+
9+
actor Main
10+
new create(env: Env) =>
11+
let a: I8 = 1
12+
let b: I8 = 3
13+
let c: I8 =
14+
a
15+
-b
16+
env.out.print(c.string())

0 commit comments

Comments
 (0)