Skip to content

Commit

Permalink
Make "sugar" runnable
Browse files Browse the repository at this point in the history
  • Loading branch information
shaedrich committed May 26, 2024
1 parent d7302cd commit 77ccaad
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 8 deletions.
31 changes: 28 additions & 3 deletions code-samples/sugar-update-additional-parameters.pony
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
foo1(2, 3) = x
foo2() = x
foo3(37, "Hello", 3.5 where a = 2, b = 3) = x
class Foo
var _x: (U32|F32)

new create(x: U32) =>
_x = x

fun ref update(x: U32 = 0, y: (String|U32) = "", z: F32 = 0.0, value: U32, b: U32 = 0, a: U32 = 0) =>
_x = (value * x).f32() + (a * b).f32() + z
match y
| let u: U32 => _x = _x.f32() + u.f32()
end

fun get_value(): (U32|F32) =>
_x

actor Main
new create(env: Env) =>
let foo1 = Foo(5)
let foo2 = Foo(5)
let foo3 = Foo(5)
let x: U32 = 10
env.out.print("foo = " + foo1.get_value().string())
foo1(2, 3) = x
foo2() = x
foo3(37, "Hello", 3.5 where a = 2, b = 3) = x
env.out.print("foo1 = " + foo1.get_value().string())
env.out.print("foo2 = " + foo2.get_value().string())
env.out.print("foo3 = " + foo3.get_value().string())
20 changes: 19 additions & 1 deletion code-samples/sugar-update-explicit.pony
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
foo.update(37 where value = x)
class Foo
var _x: U32 = 0

new create(x: U32) =>
update(x, 2)

fun ref update(x: U32, value: U32) =>
_x = value * x

fun get_value(): U32 =>
_x

actor Main
new create(env: Env) =>
let foo = Foo(5)
let x: U32 = 10
env.out.print("foo = " + foo.get_value().string())
foo.update(37 where value = x)
env.out.print("foo = " + foo.get_value().string())
20 changes: 19 additions & 1 deletion code-samples/sugar-update-implicit.pony
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
foo(37) = x
class Foo
var _x: U32 = 0

new create(x: U32) =>
update(x, 2)

fun ref update(x: U32, value: U32) =>
_x = value * x

fun get_value(): U32 =>
_x

actor Main
new create(env: Env) =>
let foo = Foo(5)
let x: U32 = 10
env.out.print("foo = " + foo.get_value().string())
foo(37) = x
env.out.print("foo = " + foo.get_value().string())
6 changes: 3 additions & 3 deletions docs/expressions/sugar.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,21 @@ The `update` sugar allows any class to use an assignment to accept data. Many la
In any assignment where the left-hand side is a function call, Pony will translate this to a call to update, with the value from the right-hand side as an extra argument. So:

```pony
--8<-- "sugar-update-implicit.pony"
--8<-- "sugar-update-implicit.pony:18:18"
```

becomes:

```pony
--8<-- "sugar-update-explicit.pony"
--8<-- "sugar-update-explicit.pony:18:18"
```

The value from the right-hand side of the assignment is always passed to a parameter named `value`. Any object can allow this syntax simply by providing an appropriate function `update` with an argument `value`.

__Does my update function have to have a single parameter that takes an integer?__ No, you can define update to take whatever parameters you like, as long as there is one called `value`. The following are all fine:

```pony
--8<-- "sugar-update-additional-parameters.pony"
--8<-- "sugar-update-additional-parameters.pony:23:25"
```

__Does it matter where `value` appears in my parameter list?__ Whilst it doesn't strictly matter it is good practice to put `value` as the last parameter. That way all of the others can be specified by position.
Expand Down

0 comments on commit 77ccaad

Please sign in to comment.