Skip to content

Raise with context parameters #3606

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Raise with context parameters #3606

wants to merge 1 commit into from

Conversation

serras
Copy link
Member

@serras serras commented Apr 13, 2025

The time has finally come to provide Raise with context parameters! Note that if we publish this library, users still need to use -Xskip-prerelease-check in their build file.

This PR is the start of the conversation of which functions we should expose using context-parameters-style. In this case I've decided to go with the following idea: minimize the amount of additional functions, while allowing people to freely define their functions as:

context(_: Raise<Something>) fun foo() = ...

More concretely, this implies:

  • No "runner functions" (either, option, etcetera) with contextual functions. The current functions work still OK with contextual parameters, since the receiver is part of context parameter resolution,
  • (Finally!) expose bindAll and mapOrAccumulate with the iterable/sequence/map being a receiver.

@serras serras requested review from tKe, raulraja, nomisRev and kyay10 April 13, 2025 08:13
@kyay10
Copy link
Collaborator

kyay10 commented Apr 13, 2025

Thanks for pinging me on this! I'll have a look really soon! I had some ideas in a repo called arrow-context, but that's out-of-date now since it was using context receivers

Copy link
Contributor

Copy link
Collaborator

@tKe tKe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌 Looks like a great first step towards supporting context-parameters.

Is it worth taking the same approach with another module for fx contextual?

Worth adding in some bridges for some of the RaiseAccumulate methods? It's not currently possible to use:

context(_: RaiseAccumulate<String>)
private fun example() {
  val a by accumulating { }
}

(although this would require making the getValue operator available outside of RaiseAccumulate)

Comment on lines +19 to +21
public typealias Raise<Error> = CoreRaise<Error>
public typealias SingletonRaise<Error> = CoreSingletonRaise<Error>
public typealias ResultRaise = CoreResultRaise
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the purpose of these to allow consumers to use only import arrow.core.raise.contextual.* and avoid clashes with arrow.core.raise.*?


context(raise: Raise<Error>) @RaiseDSL public inline fun <Error, OtherError, A> withError(
transform: (OtherError) -> Error,
@BuilderInference block: CoreRaise<OtherError>.() -> A
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@BuilderInference block: CoreRaise<OtherError>.() -> A
@BuilderInference block: Raise<OtherError>.() -> A

better to use the alias for consistency?

import arrow.core.NonEmptyList
import arrow.core.raise.*
import kotlin.experimental.ExperimentalTypeInference

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth also bringing in a typealias for RaiseAccumulate?

@nomisRev
Copy link
Member

Okay... I need to refactor my project 😂

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 this pull request may close these issues.

4 participants