Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'with' expression body type and exception handling #1571

Closed
aav opened this issue Feb 10, 2017 · 2 comments
Closed

'with' expression body type and exception handling #1571

aav opened this issue Feb 10, 2017 · 2 comments

Comments

@aav
Copy link

aav commented Feb 10, 2017

The compiler adds None type to the 'with' statement body type. (U8 | None) in the example below, although there is no path which may lead to value of type 'None'

The following code

class Disposable
  new create() => None
  fun dispose() => None
  fun value(): U8 => 42

primitive TestWith
  fun apply(): U8  =>
    with d = Disposable do 
      d.value()
    end

Leads to

aav@aav ~/p/p/withNone (master)> ponyc
Building builtin -> /usr/local/Cellar/ponyc/0.10.0/packages/builtin
Building . -> /Users/aav/pony-protobuf/pony/withNone
Error:
/Users/aav/pony-protobuf/pony/withNone/with_none.pony:8:5: function body isn't the result type
    with d = Disposable do
    ^
    Info:
    /Users/aav/pony-protobuf/pony/withNone/with_none.pony:7:16: function return type: U8 val
      fun apply(): U8  =>
                   ^
    /Users/aav/pony-protobuf/pony/withNone/with_none.pony:4:16: function body type: (U8 val | None val^)
      fun value(): U8 => 42
                   ^
    /usr/local/Cellar/ponyc/0.10.0/packages/builtin/none.pony:1:1: None val^ is not a subtype of U8 val
    primitive None is Stringable
    ^
    /usr/local/Cellar/ponyc/0.10.0/packages/builtin/none.pony:1:1: not every element of (U8 val | None val^) is a subtype of U8 val
    primitive None is Stringable
    ^

At the same type, adding 'else' section to the 'with' statement leads to "try expression never results in an error"

primitive TestWith
  fun apply(): U8  =>
    with d = Disposable do 
      d.value()
    else
      0
  end 
aav@aav ~/p/p/withNone (master)> ponyc
Building builtin -> /usr/local/Cellar/ponyc/0.10.0/packages/builtin
Building . -> /Users/aav/pony-protobuf/pony/withNone
Error:
/Users/aav/pony-protobuf/pony/withNone/with_none.pony:8:5: try expression never results in an error
    with d = Disposable do
    ^

Environment:

aav@aav ~/p/p/withNone (master) [255]> ponyc --version
0.10.0 [release]
compiled with: llvm 3.9.0 -- Apple LLVM version 8.0.0 (clang-800.0.42.1)
@jemc
Copy link
Member

jemc commented Feb 15, 2017

In the sync call, we discussed that it should be possible to avoid adding None as one of the possible types of the result value. To do this, we'd probably want to make TK_WITH a first-class construct instead of sugaring it to a TK_TRY_NO_CHECK.

We also discussed that we want to look at changing the fact that with will silently swallow errors in the with block - we think that raising the errors up out of the block would be more usable/useful.

We also discussed that we want to take a broader look at how the try and with constructs work in the language, and re-evaluate the design more holistically. I personally was proposing something that turned out to be similar to OCaml's two types of try blocks, though they have apparently been the subject of some confusion on the part of users, so we want to be careful. Either way, this broader redesign would have to be part of an RFC.

@jemc
Copy link
Member

jemc commented May 19, 2021

Closing - not yet resolved. Conversation has moved to ticket #3759.

@jemc jemc closed this as completed May 19, 2021
@jemc jemc removed enhancement New feature or request help wanted Extra attention is needed labels May 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants