Skip to content

Implicit class which extends Dynamic doesn't work #16258

@plokhotnyuk

Description

@plokhotnyuk

Compiler version

3.2.1

Minimized code

import scala.language.dynamics
import scala.collection._

object ImplicitDynamics {
  type JsonPrimitive = String | Int | Double | Boolean | None.type
  type SomeJson = mutable.Map[String, JsonPrimitive]

  def obj(values: (String, JsonPrimitive)*): SomeJson =
    (new mutable.HashMap[String, JsonPrimitive]()).addAll(values)

  implicit class Json(val underlying: SomeJson) extends Dynamic {    
    def selectDynamic(key: String): JsonPrimitive = underlying(key)
  }
}

import ImplicitDynamics._

println(Json(obj("name" -> "John", "age" -> 42)).name)
println(obj("name" -> "John", "age" -> 42).apply("name"))
println(obj("name" -> "John", "age" -> 42).name) // <-- Doesn't compile

Output

The last line doesn't compile.

Expectation

The list line should print the same result as previous 2.

I stuck with that during adding Scala 3 support for the dijon project in the following PR: jvican/dijon#202

Metadata

Metadata

Assignees

No one assigned

    Labels

    itype:bugstat:needs triageEvery issue needs to have an "area" and "itype" label

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions