All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.arainko.ducktape.internal.Constructor.scala Maven / Gradle / Ivy

There is a newer version: 0.2.5
Show newest version
package io.github.arainko.ducktape.internal

import scala.quoted.*

private[ducktape] object Constructor {
  def apply(using Quotes)(tpe: quotes.reflect.TypeRepr): quotes.reflect.Term = {
    import quotes.reflect.*

    val (repr, constructor, tpeArgs) =
      tpe match {
        case AppliedType(repr, reprArguments) => (repr, repr.typeSymbol.primaryConstructor, reprArguments)
        case notApplied                       => (tpe, tpe.typeSymbol.primaryConstructor, Nil)
      }

    // workaround for invoking constructors of singleton which in turn actually create new instances of singletons!
    if tpe.typeSymbol.flags.is(Flags.Module) then report.errorAndAbort("Cannot invoke constructor of a singleton")

    New(Inferred(repr))
      .select(constructor)
      .appliedToTypes(tpeArgs)
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy