Can I call a static java method using structural typing?

I want to do something line by line (note that I know this does not work, but my question is whether it can be made to work):

object O {
  def main(args: Array[String]) {
    val clazzname = classOf[System].getName
    val c = Class.forName(clazzname).asInstanceOf[{def currentTimeMillis: Long}]
    c.currentTimeMillis
  }
}

Is it possible? (without using reflection)

The real use case is for reading serialized protobuf messages.

+3
source share
2 answers

In short: No

I want a better answer, but as you can already see from the mailing list, this is not possible (at present). We hope that the situation will improve as support for Scala's native reflection is maturing.

+2
source

Will this work for you:

val c = new {                     
  def cm = System.currentTimeMillis  
}

, .

0

Source: https://habr.com/ru/post/1773029/


All Articles