diff options
author | Kimplul <kimi.h.kuparinen@gmail.com> | 2025-01-09 22:26:02 +0200 |
---|---|---|
committer | Kimplul <kimi.h.kuparinen@gmail.com> | 2025-01-09 22:26:02 +0200 |
commit | 598be4cd1bdd79e4859ae30291f4d65682cc672a (patch) | |
tree | 6e7e7ad537214c78049c4b3b2ee694c3b549fa4e /examples | |
parent | 6f7c2d6daa5c706d441ddc42c5c6409e5266049a (diff) | |
download | fwd-598be4cd1bdd79e4859ae30291f4d65682cc672a.tar.gz fwd-598be4cd1bdd79e4859ae30291f4d65682cc672a.zip |
initial reference checking
Diffstat (limited to 'examples')
-rw-r--r-- | examples/callback.fwd | 16 | ||||
-rw-r--r-- | examples/fib.fwd | 5 | ||||
-rw-r--r-- | examples/pure_move.fwd | 14 | ||||
-rw-r--r-- | examples/references.fwd | 22 |
4 files changed, 55 insertions, 2 deletions
diff --git a/examples/callback.fwd b/examples/callback.fwd new file mode 100644 index 0000000..f750b8b --- /dev/null +++ b/examples/callback.fwd @@ -0,0 +1,16 @@ +fwd_println(auto s); + +callee() +{ + fwd_println("Hello from callback!"); +} + +caller(*() callback) +{ + callback(); +} + +main() +{ + caller(callee); +} diff --git a/examples/fib.fwd b/examples/fib.fwd index e5014af..9bc5474 100644 --- a/examples/fib.fwd +++ b/examples/fib.fwd @@ -9,8 +9,9 @@ fib(int n, (int) res) if n1 < 2 { res(1); } else { - fib(n2 - 1) => int f1; - fib(n2 - 2) => int f2; + fwd_copy(n2) => int n3, int n4; + fib(n3 - 1) => int f1; + fib(n4 - 2) => int f2; res(f1 + f2); } } diff --git a/examples/pure_move.fwd b/examples/pure_move.fwd new file mode 100644 index 0000000..a00666e --- /dev/null +++ b/examples/pure_move.fwd @@ -0,0 +1,14 @@ +requires_pure(&() p) +{ + p(); +} + +main() +{ + 20 => int twenty; + requires_pure() &=> { + /* not allowed in pure context (though primitives should maybe + * be excluded just to make people's lives easier?) */ + twenty + 10 => int thirty; + } +} diff --git a/examples/references.fwd b/examples/references.fwd new file mode 100644 index 0000000..468f4a0 --- /dev/null +++ b/examples/references.fwd @@ -0,0 +1,22 @@ +references(&int a, &int b, () r) +{ + /* don't have assignment and not quite sure if I want to have to + * dereference references so I guess we can't really do much here at the + * moment, heh */ + r(); +} + +main() +{ + 20 => int twenty; + 30 => int thirty; + references(twenty&, thirty&) => { + 20 + 30 => int fifty; /* ok */ + }; + + /* references are not active anymore so we can reference again */ + references(twenty&, thirty&) => { + /* not ok since twenty/thirty is actively borrowed */ + twenty + thirty => int fifty; + }; +} |