简体   繁体   中英

Access companion object from case class (or vice-versa) using scala type macros

I'm trying to create a value on a companion object based on the fields of a case class.

I'd like to do something like this:

case class A(
  val a: Int,
  val b: String
) extends MyTypeMacro

I'd like for this to add to add a value to its companion object...

object A { 
  //Generate...
  val c: B = //Code value derived from case class fields...
}

Because companion objects and classes have access to each other, shouldn't I be able to accomplish this by accessing the companion object from within the case class type macro?

Also... I'd like to add 'val c' whether object A is defined or not.

Unfortunately that's not possible yet. We're playing with something along these lines, but it's very early stage, so I can't say anything definitive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM