Skip to content

A missing replacement for Scala 2 implicits in Scala 3 #12006

@therealcisse

Description

@therealcisse

Compiler version

Scala-3.0.0-RC1

Minimized code

Given the following code:

import scala.concurrent.Future

trait Req[A]
trait Response[A]

object Req {
  def async[F[_], A](fn: Req[A] => Response[A]): F[Response[A]] = ???
}

Scala 2

@main def Test() = {
  
  Req.async[Future, Int] {  implicit req =>
    
   val r = summon[Req[A]]
   assert(r eq req)
    ???
  }
  
  
}

Scala 3:

Doesn't compile. No easy way to make req implicit.

@main def Test() = {
  
  // Req.async[Future, Int] {  given req => // error
  // Req.async[Future, Int] {  req : given Req[A] => // error    
  // Req.async[Future, Int] {  req @ given Req[A] => // error    

Req.async[Future, Int] {  case req @ given Req[Int] => // compiles but very tedious

   val r = summon[Req[A]]
   assert(r eq req)
    ???
  }
  
  
}

Expectation

This should compile:

Req.async[Future, Int] {  req : given Req[A] => 
   val r = summon[Req[A]]
   assert(r eq req)
    ???
}

This is probably a stretch given req's type is not ascribed:

Req.async[Future, Int] {  given req => 
   val r = summon[Req[A]]
   assert(r eq req)
    ???
}
But this looks like it should be possible:

Req.async[Future, Int] { req : given Req[A] => 
   val r = summon[Req[A]]
   assert(r eq req)
    ???
}

Extra 1

Note that a version of this works in Scala 3 already:

@main def Test() = {
  
  
  class MyC[A]( implicit val x: Req[A]) {
    summon[Req[A]]
  }
  
  
  class MyD[A](using val x: Req[A]) { // ok
    summon[Req[A]]
  }
  
}

Extra 2

However this version is probably not consistency with Scala 3 given.

  class MyD[A](using val x: Req[A]) {  // compiles but why `using` keyword instead of `given`
    summon[Req[A]]
  }

using is used to query implicits but here it is used to define an implicit. A better and more consistency keyword here is given:

  class MyD[A](given val x: Req[A]) {  // doesn't compile
    summon[Req[A]]
  }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions