jakt icon indicating copy to clipboard operation
jakt copied to clipboard

Implement must unary operator

Open ccapitalK opened this issue 2 years ago • 4 comments

This PR adds a must keyword which acts as a unary operator that corresponds to AK's MUST macro. I'm not entirely sure if having a separate keyword for this is a good idea however, people I've talked to have stated that it might be better to fold this into the ! operator. I mostly wanted to be consistent with how AK's abstractions for Errors works (since I am planning on proposing a similar keyword for try, which would also be a unary operator). What are people's thoughts on this?

ccapitalK avatar May 21 '22 13:05 ccapitalK

I like it.

awesomekling avatar May 21 '22 14:05 awesomekling

I just realized I'll need to change this to work with our new way of handling errors (with try catch). I think what would make more sense is to make it something like a must block. Something like:

function main() {
  try {
    function_that_can_fail1()
    function_that_can_fail2()
  } catch error {
    println("Caught error {}", error.code())
  }

  must {
    function_that_can_fail1()
    ...
    function_that_can_fail2()
    ...
  }
}

ccapitalK avatar May 21 '22 14:05 ccapitalK

Yeah, it could be nice to have must { xyz }, but let's also support must xyz. :)

awesomekling avatar May 21 '22 14:05 awesomekling

The syntax of the proposed must keyword has been updated. It now can be used in two forms:

  • The must unary operator, used like let x = must foo();, y + must(x + 3) + must 4, etc.
  • Must blocks, which are like try blocks without the corresponding catch:
    must {
        foo();
        bar();
        baz();
    }

ccapitalK avatar May 29 '22 01:05 ccapitalK

Closed as this is a change for the now-gone Rust-based compiler. Feel free to port your changes to the new compiler, if they're not already added, and open a new PR. :^)

AtkinsSJ avatar Aug 26 '22 22:08 AtkinsSJ