Skip to content

Conversation

@iRevive
Copy link
Contributor

@iRevive iRevive commented Oct 27, 2025

Motivation

With the recently released #669, there is a new scenario where the context can be extended, and it can be passed implicitly to inner functions.

Let's consider the following example:

class Ctx(...) 
class TracingMetadata(tracingCtx: Ctx, metadata: io.grpc.Metadata)

class Service extends ServiceFs2GrpcTrailers[IO, TracingMetadata] {

  def endpoint(request: Request, ctx: TracingMetadata): IO[Response] = {
    given Ctx = ctx.tracing
    for {
      _ <- log("handling request")
      r <- logic(request)
    } yield r
  }
  
  def logic(r: Request)(using Ctx): IO[Response] = ???
  def log(str: String)(using Ctx): IO[Unit] = ???
  
}

The usage is a little noisy and repetitive. With the new flag, we can put ctx into an implicit position in the generated service:

given (using m: Meta): Ctx = m.c

class Service extends ServiceFs2GrpcTrailers[IO, TracingMetadata] {

  def endpoint(request: Request)(using ctx: TracingMetadata): IO[Response] = 
    for {
      _ <- log("handling request")
      r <- logic(request)
    } yield r
  
  def logic(r: Request)(using Ctx): IO[Response] = ???
  def log(str: String)(using Ctx): IO[Unit] = ???
  
}

@ahjohannessen
Copy link
Collaborator

ahjohannessen commented Oct 28, 2025

I think the idea of opt-in for generation of using/implicits is nice wrt. ergonimics.

@iRevive iRevive force-pushed the topic/ctx-as-implicit-param branch from 10f747f to 3937918 Compare October 30, 2025 05:51
@iRevive iRevive force-pushed the topic/ctx-as-implicit-param branch from 7ed2485 to 4875fd9 Compare October 31, 2025 14:33
@iRevive
Copy link
Contributor Author

iRevive commented Oct 31, 2025

@ahjohannessen this one is rebased and squashed.

@iRevive iRevive requested a review from ahjohannessen October 31, 2025 14:35
@ahjohannessen ahjohannessen merged commit 98143c2 into typelevel:main Nov 1, 2025
5 checks passed
@ahjohannessen
Copy link
Collaborator

Great work! 👍

@iRevive iRevive deleted the topic/ctx-as-implicit-param branch November 1, 2025 11:43
@iRevive
Copy link
Contributor Author

iRevive commented Nov 1, 2025

@ahjohannessen thanks!

What's your plan for the release? Should we have the RC first?

@ahjohannessen
Copy link
Collaborator

ahjohannessen commented Nov 1, 2025

@iRevive I have released v3.0.0. Do not think a RC is necessary. The work you have done is opt-in, so it is not that disruptive.

@iRevive
Copy link
Contributor Author

iRevive commented Nov 1, 2025

@iRevive I have released v3.0.0. Do not think a RC is necessary.

Perfect, thank you!

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.

2 participants