Scala Native now supports reflective instantiation

Hello everyone!

At the Scala Center we are happy to announce that reflective instantiation has been merged to the Scala Native master branch.

Reflective instantiation allows libraries and frameworks to reflectively instantiate modules and classes that are annotated with the @EnableReflectiveInstantiation annotation.

Here is a simple usage example:

import scala.scalanative.reflect.Reflect
import scala.scalanative.reflect.annotation.EnableReflectiveInstantiation

object Main {
  def main(args: Array[String]): Unit = {
    val optClassData = Reflect.lookupInstantiatableClass("Foo")
    val classData = optClassData.get

    val ctor = classData.getConstructor(classOf[Int], classOf[Bar]).get
    val instance = ctor.newInstance(42, new Bar).asInstanceOf[Foo]

    println(s"instance = $instance")
  }
}

class Bar

@EnableReflectiveInstantiation
class Foo(val x: Int, val bar: Bar) {
  println(s"Creating a Foo: x = $x, bar = $bar")

  override def toString: String = s"Foo($x, $bar)"
}

We believe that the above feature will be useful for frameworks that utilise reflection and we are looking forward to people using it and reporting any bugs found.

It is also worth noting that the Scala Centre Team is working on bringing the road-map for the Scala Native project, so stay tuned!

20 Likes

Looking forward to the road map of the Scala native!