lampepfl / dotty-feature-requests

Historical feature requests. Please create new feature requests at https://github.com/lampepfl/dotty/discussions/new?category=feature-requests
31 stars 2 forks source link

Automatically Adapt By Name Parameters #234

Closed adamgfraser closed 1 year ago

adamgfraser commented 3 years ago

Currently the code below does not compile. printLine takes a by name argument whereas flatMap expects a function that takes an argument that is not by name. We can resolve the compilation error by replacing printLine with printLine(_) but it would be nice if the compiler allowed us to do this without the additional noise.

trait ZIO[-R, +E, +A]:
  def flatMap[R1 <: R, E1 >: E, B](f: A => ZIO[R1, E1, B]): ZIO[R1, E1, B] =
    ???

def printLine(line: => Any): ZIO[Any, Nothing, Unit] =
  ???

val zio1: ZIO[Any, Nothing, Int] =
  ???

val zio2 = zio1.flatMap(printLine)

// Found:    (=> Any) => ZIO[Nothing, Any, Any]
// Required: Int => ZIO[Nothing, Any, Any]
SethTisue commented 3 years ago

I didn't turn up an identical Scala 2 ticket in scala/bug. but I'll note https://github.com/scala/bug/issues/9313 in passing anyway, since it's related in the sense that it's also about the (IMO) rather murky, semi-supported nature of by-name parameters to function values

bbarker commented 3 years ago

Cross-referencing proposal on Discourse: Make by-name parameters just sugar syntax rather than actual types