aboutsummaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
Diffstat (limited to 'README.md')
-rw-r--r--README.md29
1 files changed, 15 insertions, 14 deletions
diff --git a/README.md b/README.md
index 3fc6286..6703bcb 100644
--- a/README.md
+++ b/README.md
@@ -6,7 +6,7 @@ Puck is an experimental, memory safe, structurally typed, interface-first, imper
It aims to be clean and succinct while performant: inspired by the syntax and metaprogramming of [Nim](https://nim-lang.org/), the error handling of [Swift](https://www.swift.org/), the performance and safety guarantees of [Rust](https://www.rust-lang.org/), the async/await and comptime of [Zig](https://ziglang.org/), and the module system of [OCaml](https://ocaml.org/).
<details>
-<summary><b>Example: Interfaces</b></summary>
+<summary><b>Example: Type Classes</b></summary>
```nim
# Note: These declarations are adapted from the standard prelude.
@@ -16,8 +16,8 @@ pub type Result[T, E] = union
Okay(T)
Error(E)
-## The Err interface. Useful for dynamically dispatching errors.
-pub type Err = interface
+## The Err class. Useful for dynamically dispatching errors.
+pub type Err = class
str(Self): str
dbg(Self): str
@@ -51,24 +51,25 @@ pub type Expr = ref union
then_branch: Expr, else_branch: Expr)
## Evaluate an Expr down to a Value, or return an Error.
-pub func eval(context: mut HashTable[Ident, Value], expr: Expr): Result[Value]
+pub func eval(context: mut HashTable[Ident, Value], expr: Expr): Result[Value] =
match expr
- of Literal(value): Okay(value)
- of Variable(ident):
+ of Literal(value) then Okay(value)
+ of Variable(ident) then
context.get(ident)
- .err("Could not find variable {} in context!".fmt(ident))
- of Application(body, arg):
- if body of Abstraction(param, body as inner_body):
+ .err("Could not find variable {} in context!"
+ .fmt(ident))
+ of Application(body, arg) then
+ if body of Abstraction(param, body as inner_body) then
context.set(param, context.clone.eval(arg)?)
context.eval(inner_body)
- else:
+ else
Error("Expected Abstraction, found body {} and argument {}".fmt(body, arg))
- of Conditional(condition, then_branch, else_branch):
- if context.clone.eval(condition)? == "true":
+ of Conditional(condition, then_branch, else_branch) then
+ if context.clone.eval(condition)? == "true" then
context.eval(then_case)
- else:
+ else
context.eval(else_case)
- of _: Error("Invalid expression {}".fmt(expr))
+ of _ then Error("Invalid expression {}".fmt(expr))
```
</details>