You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
"Unwrapping" statements (backwards arrow, output is bound to the (irrefutable) pattern on the left)
"Executing" statements (no arrow, output is ignored)
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.
The text was updated successfully, but these errors were encountered:
Types like
Option
andResult
support the?
operator, andFutures
can beawait
ed.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 isio!
. It's also more explicit about only handlingIO
and not arbitrary monads.It should look somewhat like this:
The above code gets translated to:
Perhaps it is also useful to allow let bindings. In that case there would be three kinds of 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 meanpure
, but without the function call parenthesis. For example:would be the same as:
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.
The text was updated successfully, but these errors were encountered: