Skip to content

[Scala3] Add Instantiate API#5278

Open
adkian-sifive wants to merge 3 commits intomainfrom
adkian-sifive/scala3-instantiate
Open

[Scala3] Add Instantiate API#5278
adkian-sifive wants to merge 3 commits intomainfrom
adkian-sifive/scala3-instantiate

Conversation

@adkian-sifive
Copy link
Copy Markdown
Member

@adkian-sifive adkian-sifive commented Apr 22, 2026

Contributor Checklist

  • Did you add Scaladoc to every public function/method?
  • Did you add at least one test demonstrating the PR?
  • Did you delete any extraneous printlns/debugging code?
  • Did you specify the type of improvement?
  • Did you add appropriate documentation in docs/src?
  • Did you request a desired merge strategy?
  • Did you add text to be included in the Release Notes for this change?

Type of Improvement

  • Feature (or new API)

Desired Merge Strategy

  • Squash: The PR will be squashed and merged (choose this if you have no preference).

Release Notes

Reviewer Checklist (only modified by reviewer)

  • Did you add the appropriate labels? (Select the most appropriate one based on the "Type of Improvement")
  • Did you mark the proper milestone (Bug fix: 3.6.x, 5.x, or 6.x depending on impact, API modification or big change: 7.0)?
  • Did you review?
  • Did you check whether all relevant Contributor checkboxes have been checked?
  • Did you do one of the following when ready to merge:
    • Squash: You/ the contributor Enable auto-merge (squash) and clean up the commit message.
    • Merge: Ensure that contributor has cleaned up their commit history, then merge with Create a merge commit.

@adkian-sifive adkian-sifive added the Scala 3 Changes related to upgrading to Scala 3 label Apr 22, 2026
@adkian-sifive adkian-sifive changed the base branch from main to adkian-sifive/scala3-di April 22, 2026 00:01
Comment on lines +171 to +173
val sourceInfoExpr = Expr.summon[SourceInfo].getOrElse {
report.errorAndAbort("Instantiate.apply requires an implicit SourceInfo")
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just thread this through the user API instead so the user can pass it manually if they want?


def definitionMacro[A <: BaseModule: Type](con: Expr[A])(using q: Quotes): Expr[core.Definition[A]] = {
import q.reflect.*
val (argsTuple, constructorFunc) = extractAndBuild[A](con)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does instanceMacro have a source locator but this doesn't?

private object InstantiateIntfMacros {
import scala.quoted.*

private def extractAndBuild[A <: BaseModule: Type](
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment describing what this returns? It appears to be the arguments as a tuple and then the constructor function value, but it'd be good to have that in English.

con: Expr[A]
)(using q: Quotes): (Expr[Any], Expr[_]) = {
import q.reflect.*
def unwrapInlined(term: Term): Term = term match {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment describing would be good

Comment on lines +61 to +70
val isImplicit = inner.tpe.widen match {
case mt: MethodType => mt.isImplicit
case _ => false
}

if (isImplicit) {
collectArgs(inner, accFlat, accStructured)
} else {
collectArgs(inner, newArgs ::: accFlat, newArgs :: accStructured)
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we dropping implicit arguments? That does not seem right

Comment on lines +78 to +81
case Select(New(tpt), _) =>
(tpt, None, unwrapped)
case TypeApply(Select(New(tpt), _), targs) =>
(tpt, Some(targs), unwrapped)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are these two cases?

}
).asExpr
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the mix of vals and defs hard to follow, can you collect all of the defs separately from the vals? Maybe as separate private defs outside of extractAndBuild or at least at the top of the function?

Comment on lines +147 to +152
case List(t1, t2) =>
AppliedType(TypeRepr.of[Tuple2].typeSymbol.typeRef, List(t1, t2))
case List(t1, t2, t3) =>
AppliedType(TypeRepr.of[Tuple3].typeSymbol.typeRef, List(t1, t2, t3))
case _ =>
AppliedType(defn.TupleClass(n).typeRef, argTypes)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it standard to have to handle tuple 2 and tuple 3 specially? If so please add a comment.

buildConstructorLambdaMultiList(
List(tupleTypeRepr),
params => {
val flatArgs = (1 to n).map(i => Select.unique(params.head.asInstanceOf[Term], s"_$i")).toList
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The repeated use of params.head.asInstanceOf[Term] looks like a bug, I'm assuming it isn't. Can you hoist it out of the map and give it a meaningful name and possibly comment describing what it is?

Base automatically changed from adkian-sifive/scala3-di to main May 5, 2026 01:33
@adkian-sifive adkian-sifive force-pushed the adkian-sifive/scala3-instantiate branch from 99b0784 to 26fc1c9 Compare May 5, 2026 01:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scala 3 Changes related to upgrading to Scala 3

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants