-
-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The `with` keyword has been little used in most Pony programs. `with` was implemented as "sugar" over the `try/else/then` construct. Over the course of time, this became problematic as changes were made to make `try` more friendly. However, these `try` changes negatively impacted `with`. Prior to this change, `with` had become [barely usable](#3759) . We've reimplemented `with` to address the usability problems that built up over time. `with` is no longer sugar over `try` and as such, shouldn't develop any unrelated problems going forward. However, the reimplemented `with` is a breaking change. Because `with` was sugar over `try`, the full expression was `with/else`. Any error that occurred within a `with` block would be handled by provided `else`. The existence of `with/else` rather than pure `with` was not a principled choice. The `else` only existed because it was needed to satisfy error-handling in the `try` based implementation. Our new implementation of `with` does not have the optional `else` clause. All error handling is in the hands of the programmer like it would be with any other Pony construct. Previously, you would have had: ```pony with obj = SomeObjectThatNeedsDisposing() do // use obj else // only run if an error has occurred end ``` Now, you would do: ```pony try with obj = SomeObjectThatNeedsDisposing() do // use obj end else // only run if an error has occurred end ``` Or perhaps: ```pony with obj = SomeObjectThatNeedsDisposing() do try // use obj else // only run if an error has occurred end end ``` The new `with` block guarantees that `dispose` will be called on all `with` condition variables before jumping away whether due to an `error` or a control flow structure such as `return`. This first version of the "new `with`" maintains one weakness that the previous implementation suffered from; you can't use an `iso` variable as a `with` condition. The following code will not compile: ```pony use @pony_exitcode[None](code: I32) class Disposable var _exit_code: I32 new iso create() => _exit_code = 0 fun ref set_exit(code: I32) => _exit_code = code fun dispose() => @pony_exitcode(_exit_code) actor Main new create(env: Env) => with d = Disposable do d.set_exit(10) end ``` A future improvement to the `with` implementation will allow the usage of `iso` variables as `with` conditions. Internally now, we have a new abstract token `TK_DISPOSING_BLOCK` that is used to implement `with`. The code for it is still very similar to `try` except, it doesn't have an `else` clause. Instead, it has a body and a dispose clause that correspond to the body and then clauses in `try`. Fixes #3759
- Loading branch information
1 parent
b2bf395
commit cd7d10a
Showing
38 changed files
with
589 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
## Reimplement `with` | ||
|
||
The `with` keyword has been little used in most Pony programs. `with` was implemented as "sugar" over the `try/else/then` construct. Over the course of time, this became problematic as changes were made to make `try` more friendly. However, these `try` changes negatively impacted `with`. Prior to this change, `with` had become [barely usable](https://github.com/ponylang/ponyc/issues/3759). We've reimplemented `with` to address the usability problems that built up over time. `with` is no longer sugar over `try` and as such, shouldn't develop any unrelated problems going forward. However, the reimplemented `with` is a breaking change. | ||
|
||
Because `with` was sugar over `try`, the full expression was `with/else`. Any error that occurred within a `with` block would be handled by provided `else`. The existence of `with/else` rather than pure `with` was not a principled choice. The `else` only existed because it was needed to satisfy error-handling in the `try` based implementation. Our new implementation of `with` does not have the optional `else` clause. All error handling is in the hands of the programmer like it would be with any other Pony construct. | ||
|
||
Previously, you would have had: | ||
|
||
```pony | ||
with obj = SomeObjectThatNeedsDisposing() do | ||
// use obj | ||
else | ||
// only run if an error has occurred | ||
end | ||
``` | ||
|
||
Now, you would do: | ||
|
||
```pony | ||
try | ||
with obj = SomeObjectThatNeedsDisposing() do | ||
// use obj | ||
end | ||
else | ||
// only run if an error has occurred | ||
end | ||
``` | ||
|
||
Or perhaps: | ||
|
||
```pony | ||
with obj = SomeObjectThatNeedsDisposing() do | ||
try | ||
// use obj | ||
else | ||
// only run if an error has occurred | ||
end | ||
end | ||
``` | ||
|
||
The new `with` block guarantees that `dispose` will be called on all `with` condition variables before jumping away whether due to an `error` or a control flow structure such as `return`. | ||
|
||
This first version of the "new `with`" maintains one weakness that the previous implementation suffered from; you can't use an `iso` variable as a `with` condition. The following code will not compile: | ||
|
||
```pony | ||
use @pony_exitcode[None](code: I32) | ||
class Disposable | ||
var _exit_code: I32 | ||
new iso create() => | ||
_exit_code = 0 | ||
fun ref set_exit(code: I32) => | ||
_exit_code = code | ||
fun dispose() => | ||
@pony_exitcode(_exit_code) | ||
actor Main | ||
new create(env: Env) => | ||
with d = Disposable do | ||
d.set_exit(10) | ||
end | ||
``` | ||
|
||
A future improvement to the `with` implementation will allow the usage of `iso` variables as `with` conditions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.