-
Notifications
You must be signed in to change notification settings - Fork 0
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
Add monadic val #2
base: master
Are you sure you want to change the base?
Conversation
Here is another example of using the monadic val syntax: trait Monad[+A] {
def flatMap[B](f: A => Monad[B]): Monad[B]
}
def pure[A](a: A): Monad[A] = ???
val foo = 42
val ma: Monad[Int] = ???
val x: Monad[String] = {
val a: Int <- ma
println("real side effect")
val b <- ma
if (a == b) {
pure("foo")
} else {
pure("bar")
}
} |
03b3264
to
167c607
Compare
167c607
to
c835f02
Compare
Does the first statement of the block have to be a |
No, since the transformation really just desugars to |
Also, all statements except for monadic binds just preserve their semantics. So, what you read is what you get ;) |
It would be nice to have syntax for monadic binds that ignore their results. That is, those which are only executed for their side effects. Currently one has to write: {
...
val _ <- log("foo")
val _ <- log("bar")
..
} But I don't have a good proposal for a syntax there, yet. The best I could come up with is using {
...
do log("foo")
do log("bar")
..
} While it nicely fits as a marker that the rhs is monadic and is reminiscent of Haskell, it might not ideal to overload it as a keyword. It also will clash syntactically with Any other ideas? |
WIP
Adding monadic value bindings to block syntax. That is,
desugars to:
More details can be found in the contributors forum.