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

Do-notation macro #4

Open
RileyApeldoorn opened this issue Sep 10, 2022 · 0 comments · May be fixed by #5
Open

Do-notation macro #4

RileyApeldoorn opened this issue Sep 10, 2022 · 0 comments · May be fixed by #5
Assignees

Comments

@RileyApeldoorn
Copy link
Owner

RileyApeldoorn commented Sep 10, 2022

Types like Option and Result support the ? operator, and Futures can be awaited. IO can make use of neither of those and needs some other way to make the code using it even remotely readable. For this, we'll replicate Haskell's do-notation sugar using a macro. It allows programmers to work with monads without littering the code with the bind operator.

Since do is a reserved keyword, an appropriate second best option is io!. It's also more explicit about only handling IO and not arbitrary monads.

It should look somewhat like this:

let x: IO<'_> = io! {
    contents <- read_to_string("test.txt");
    println(contents)
};

The above code gets translated to:

let x: IO<'_> = read_to_string("test.txt").bind(|contents| {
    println(contents)
});

Perhaps it is also useful to allow let bindings. In that case there would be three kinds of statements:

  1. "Unwrapping" statements (backwards arrow, output is bound to the (irrefutable) pattern on the left)
  2. "Executing" statements (no arrow, output is ignored)
  3. Let-binding statements

Every line in a block, except for the last, must be semicolon-terminated.

The last expression of the block must be the expected type for the whole block. If desired, the return keyword can be overloaded in the last expression to mean pure, but without the function call parenthesis. For example:

io! {
    return "hello"
}

would be the same as:

pure("hello")

Since this seems pretty ungodly to do with declarative macros, it might be nice to put this behind a feature flag since it will introduce dependencies.

@RileyApeldoorn RileyApeldoorn linked a pull request Sep 14, 2022 that will close this issue
@RileyApeldoorn RileyApeldoorn self-assigned this Sep 16, 2022
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

Successfully merging a pull request may close this issue.

1 participant