Skip to content

extension methods not resolving when same name in scope for different types #24241

@joan38

Description

@joan38

Compiler version

3.7.3-RC1

Minimized code

extension (long1: Option[Long])
  def +(long2: Option[Long]): Option[Long] = (long1, long2) match
    case (Some(long1), Some(long2)) => Some(long1 + long2)
    case (Some(long1), None)        => Some(long1)
    case (None, Some(long2))        => Some(long2)
    case (None, None)               => None


case class Price(usd: Option[Long] = None)

object Price:
  extension (price1: Price)
    def +(price2: Price): Price = Price(usd = price1.usd + price2.usd)

https://scastie.scala-lang.org/09Hol8TDSl6bNeMjvcW6bw

Output

value + is not a member of Option[Long].
An extension method was tried, but could not be fully constructed:

    Playground.Price.+(price1.usd)

    failed with:

        Found:    (price1.usd : Option[Long])
        Required: Playground.Price

Expectation

This should have resolved the + on Option[Long]. The following works for example:

object options:
  extension (long1: Option[Long])
    def +(long2: Option[Long]): Option[Long] = (long1, long2) match
      case (Some(long1), Some(long2)) => Some(long1 + long2)
      case (Some(long1), None)        => Some(long1)
      case (None, Some(long2))        => Some(long2)
      case (None, None)               => None


case class Price(usd: Option[Long] = None)

object Price:
  extension (price1: Price)
    def +(price2: Price): Price = Price(usd = options.+(price1.usd)(price2.usd))

https://scastie.scala-lang.org/NyjdElF5SCaqZCSQLDnJNg

Thanks

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