Skip to content

Scala 3 eagerly resolves type aliases in function argument types #2200

@xerial

Description

@xerial
object MyMacros {
  def typeNameOf[A: Type](using Quotes): Expr[String] = {
    val name = Type.show[A]
    Expr(name)
  }
}

object Test extends App {
  inline def typeNameOf[A]: String = ${
    MyMacros.typeNameOf[A]
  }

  inline def typeNameOfF1[A](f: A => Unit): String = ${
    MyMacros.typeNameOf[A]
  }

  type MyString = String

  println(typeNameOf[MyString]) 
  // MyString (OK)
  println(typeNameOfF1 { (x: MyString) =>  })   
  // java.lang.String <---------- NG: This should be MyString
  println(typeNameOfF1[MyString] { (x: MyString) =>  })  
  // MyString <--- OK. If the type name is given as type param, the alias is preserved 
  
  println(typeNameOfF1 { (x: Seq[MyString]) => })
  // scala.colleciton.immutable.Seq[MyString]
  prinrln(typeNameOfF1 { (x: (MyString, Int)) => })
  // scala.Tuple2[MyString, scala.Int]
}

Scala 3 (3.1.2) shows inconsistent behavior in resolving type aliases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Done

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions