Skip to content

Commit

Permalink
Make "annotations" runnable
Browse files Browse the repository at this point in the history
  • Loading branch information
shaedrich authored May 27, 2024
1 parent c4cdf7d commit 00bc87f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ class Foo
fun foo[A: Any](a: (A | Empty val)) =>
match consume a
| let a': A => None
end
end

actor Main
new create(env: Env) =>
let foo: Foo = Foo
env.out.print(foo.foo[Any]("Something").string())
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
if \likely\ cond then
foo
end
type T is (U32|U8)

while \unlikely\ cond then
bar
end
actor Main
new create(env: Env) =>
let foo = "foo"
let bar = "bar"
let baz = "baz"
var cond = true
let obj: U32 = 42
let expr: U32 = 42

if \likely\ cond then
foo
end

repeat
baz
until \likely\ cond end
cond = false
while \unlikely\ cond do
bar
end

match obj
| \likely\ expr => foo
| \unlikely\ let capt: T => bar
end
cond = true
repeat
baz
until \likely\ cond end

let res =
match obj
| \likely\ expr => foo
| \unlikely\ let capt: T => bar
end

env.out.print("res = " + res)
10 changes: 9 additions & 1 deletion code-samples/appendices-annotations-packed-annotation.pony
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
struct \packed\ MyPackedStruct
var x: U8
var y: U32
var y: U32

new create() =>
x = 0
y = 1

actor Main
new create(env: Env) =>
env.out.print("{\n\t\"x\": " + MyPackedStruct.x.string() + ",\n\t\"y\": " + MyPackedStruct.y.string() + "\n}")
11 changes: 8 additions & 3 deletions docs/appendices/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,20 @@ The following annotations are recognised by the Pony compiler. Note that the Pon
Recognised on a `struct` declaration. Removes padding in the associated `struct`, making it ABI-compatible with a packed C structure with compatible members (declared with the `__attribute__((packed))` extension or the `#pragma pack` preprocessor directive in many C compilers).

```pony
--8<-- "appendices-annotations-packed-annotation.pony"
--8<-- "appendices-annotations-packed-annotation.pony:1:3"
```

#### `likely` and `unlikely`

Recognised on a conditional expression (`if`, `while`, `until` and `|` (as a pattern matching case)). Gives optimisation hints to the compiler on the likelihood of a given conditional expression.

```pony
--8<-- "appendices-annotations-likely-and-unlikely-annotations.pony"
--8<--
appendices-annotations-likely-and-unlikely-annotations.pony:12:14
appendices-annotations-likely-and-unlikely-annotations.pony:17:19
appendices-annotations-likely-and-unlikely-annotations.pony:22:24
appendices-annotations-likely-and-unlikely-annotations.pony:27:30
--8<--
```

### `nodoc`
Expand All @@ -85,7 +90,7 @@ The above code won't compile because you could supply `Empty ref`. Doing so resu
By adding `nosupertype` to the definition of `Empty`, we declare that `Empty` is not a subtype of `Any` and thereby allow the code to compile as there is no longer an unsafe match.

```pony
--8<-- "appendices-annotations-empty-with-nosupertype-annotation.pony"
--8<-- "appendices-annotations-empty-with-nosupertype-annotation.pony:1:7"
```

`nosupertype` is particularly valuable when constructing generic classes like collections that need a marker class to describe "lack of an item".

0 comments on commit 00bc87f

Please sign in to comment.