Using λ as type name leads to error when pattern matching

I use “λ” as name of a case class:

    object O:
        case class λ(value: Any) { def asSymbol = value.asInstanceOf[Symbol] }
    import O.*

But when I put it between square brackets List[λ] it leads to error:

    scala> λ(List(λ(Symbol("a")))) match { case λ(xs: List[λ]) => xs.map(_.asSymbol) }
    -- [E008] Not Found Error: -----------------------------------------------------
    1 |λ(List(λ(Symbol("a")))) match { case λ(xs: List[λ]) => xs.map(_.asSymbol) }
      |                                                              ^^^^^^^^^^
      |                                       value asSymbol is not a member of λ
    1 error found

The way out is to qualify λ as O.λ. I know λ is used in type names like | or & are in union or intersection types.

Can it not be reconciled except as O.λ?

I’m not sure about the overall rules for symbolic names, but in this case using backticks for the type can be a workaround for this limitation/bug

??? match { case λ(xs: List[`λ`]) => xs.map(v => v.asSymbol) }
1 Like

@sjbiaga

Lower-case type variables in type patterns mean defining a new type variable, not referring to an already defined type variable.

Compare:

You can fix the lower-case t code with backticks: Scastie - An interactive playground for Scala. (case t(xs: List[`t`])).

λ is lower-case. Upper-case would be Λ.

1 Like

Thanks.

backticks for types - never would I have thought of it. thanks, i’ll poke around some more.