Skip to content

Proposal for replacing IsXxx extractors in tasty-reflect #7204

Closed
@smarter

Description

@smarter

https://github.com/lampepfl/dotty/blob/master/library/src/scala/tasty/reflect/TreeOps.scala uses the following pattern:

object IsImport {
  def unapply(tree: Tree) given (ctx: Context): Option[Import] =
    internal.matchImport(tree)
}

object Import {
  // ...
  def unapply(tree: Tree) given (ctx: Context): Option[(Boolean, Term, List[ImportSelector])] =
    internal.matchImport(tree).map(x => (x.importImplied, x.expr, x.selectors))
}

Which leads to usesites like this:

tree match {
  case IsImport(tree) =>
    Import.copy(tree)(...)
  // ...
}

The use of IsImport does not feel very natural, I think the following pattern could be used instead:

object Import {
  // ...
  def unapply(tree: Import @unchecked) given (ctx: Context): Option[(Boolean, Term, List[ImportSelector])] =
    internal.matchImport(tree).map(x => (x.importImplied, x.expr, x.selectors))
}
tree match {
  case tree @ Import(_) =>
    Import.copy(tree)(...)
  // ...
}

Note the use of @unchecked at the definition site to avoid unchecked warnings at use-site, this is still safe since the pattern won't match if the tree isn't actually an Import node.

WDYT @nicolasstucki ?

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