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

Extension methods for await/unlift/reflect #83

Closed
cornerman opened this issue Jun 17, 2024 · 6 comments
Closed

Extension methods for await/unlift/reflect #83

cornerman opened this issue Jun 17, 2024 · 6 comments

Comments

@cornerman
Copy link

It would be great to have chainable methods for awaiting a value inside an async block like this:

async {
  Future(1).await + Future(2).await
}

I find it a nice because it requires less braces than await(Future(1)). Furthermore, it would support the syntax from https://github.com/typelevel/cats-effect-cps here. So, it very easy to switch.

As far as I understand, this could be achieved by making await/unlift/reflect extension methods on F[T], defining it like this:

extension[F[_],T,G[_]](f: F[T]) {
  def await(using ctx: CpsMonadContext[G], conversion: CpsMonadConversion[F,G]): T = ???
}

Afaik, it should then be available as method await(f) and as an extension f.await. What do you think?

@cornerman
Copy link
Author

When talking about short syntax: I actually really like the short syntax !. But now I stumbled on a case of !IO(true) and felt a bit confused about it. What do you think about using ~ instead (or additionally?). This is bit-wise negation, which I actually never used before - so maybe it would be less likely to have a clash of meanings.

@rssh
Copy link
Owner

rssh commented Jun 17, 2024

Yes, it (postfix await) makes sense.
Two questions
- can this break 'cps-async-effect' ? (they do not reexport imports, but sometimes compiler leak imports, don't know why exactly. Will. recheck. )
- SIP54 ( https://docs.scala-lang.org/sips/multi-source-extension-overloads.html ) will be enabled by default in the next minor version of a compiler. If we want to coexists with other postfix awaits, better wait for this version.

@rssh
Copy link
Owner

rssh commented Sep 7, 2024

Oops -- looks like in 3.5.0 we can't define an extension with the same name as a method,
because the compiler begins to understand prefix invocation as an overloaded application.

I will submit it to the compiler team, but I am unsure if it will be classified as unexpected behavior.

@rssh
Copy link
Owner

rssh commented Sep 7, 2024

Hmm, maybe we can use only extensions instead ...yes, this works

@cornerman
Copy link
Author

Awesome, I am very happy that it works :)

@rssh
Copy link
Owner

rssh commented Sep 7, 2024

0.9.22. with await extension method is published on maven central

@rssh rssh closed this as completed Sep 7, 2024
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

2 participants