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 method overload not picked up when parameter is conditional #20335

Open
ghostdogpr opened this issue May 5, 2024 · 2 comments
Open
Labels
area:implicits related to implicits itype:bug

Comments

@ghostdogpr
Copy link
Contributor

ghostdogpr commented May 5, 2024

Compiler version

3.4.1

Minimized code

import java.time.OffsetDateTime
import scala.concurrent.duration.*

val dateTime = OffsetDateTime.now()

implicit class DateTimeOps(val dateTime: OffsetDateTime) extends AnyVal {
  def plus(amount: FiniteDuration): OffsetDateTime =
    dateTime
}

dateTime plus Duration.Zero // ok
dateTime plus (if (true) Duration.Zero else Duration.Zero) // not ok

Scastie: https://scastie.scala-lang.org/YNoWvmUYTHSWmgpHzWwj9A

Output

Found:    (scala.concurrent.duration.Duration.Zero :
  scala.concurrent.duration.FiniteDuration)
Required: java.time.temporal.TemporalAmount

Expectation

The same code compiles well with Scala 2: https://scastie.scala-lang.org/lfVKMgn4RRWpelXheh5rjQ
I tried using extension but the result is the same.
Note that this happens when there is already a method of the same name defined on the class (plus in my example exists in OffsetDateTime with different parameters).

@ghostdogpr ghostdogpr added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels May 5, 2024
@ghostdogpr ghostdogpr changed the title Extension method not picked up when parameter is conditional Extension method overload not picked up when parameter is conditional May 5, 2024
@nox213
Copy link
Contributor

nox213 commented May 8, 2024

If I modify second case like this, it compiles.

dateTime plus ((if (true) Duration.Zero else Duration.Zero): FiniteDuration)

Mabye the issue is related with type inference.

@som-snytt
Copy link
Contributor

also OffsetDateTime#plus is overloaded. But I wasn't able to minimize quickly. Maybe there are implicits on Duration involved.

@Gedochao Gedochao added area:infer area:implicits related to implicits and removed stat:needs triage Every issue needs to have an "area" and "itype" label area:infer labels May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:implicits related to implicits itype:bug
Projects
None yet
Development

No branches or pull requests

4 participants