3

勝手なタイトル失礼します。このようなものに遭遇したことがないので、質問を簡潔に表現する方法が完全にはわかりません。


背景情報:

私は次の特性を持っています。型はShapeless 拡張可能なレコードU型を保持するためのものです:

trait Flattened[T] {
  type U <: shapeless.HList
  def fields: U
}

ブラックボックス マクロを使用して (この質問の範囲外の理由で)、特性の新しいインスタンスを作成しています。

object flatten {

  import scala.language.experimental.macros
  import scala.reflect.macros.blackbox.Context

  def apply[T](struct: T): Flattened[T] =
    macro impl[T]

  def impl[T : c.WeakTypeTag](c: Context)(struct: c.Tree): c.Tree = {
    import c.universe._

    // AST representing a Shapeless extensible record (actual
    // implementation uses values in `struct` to construct this
    // AST, but I've simplified it for this example).
    val fields: Tree = q"""("a" ->> 1) :: ("b" ->> "two") :: ("c" ->> true) :: HNil"""

    // Result type of the above AST
    val tpe: TypeTree = TypeTree(c.typecheck(q"$fields").tpe)

    q"""
    new Flattened[${weakTypeOf[T]}] {
      import shapeless._
      import syntax.singleton._
      import record._

      type U = $tpe
      val fields = $fields
    }
    """
  }
}


問題:

問題は、このマクロを使用して の新しいインスタンスを作成するとFlattened、 のタイプがfields拡張可能なレコードではなくなることです。

import shapeless._
import syntax.singleton._
import record._

val s = "some value... it doesn't matter for this example, since it isn't used. I'm just putting something here so the example compiles and runs in a REPL."
val t = flatten(s)

val fields = t.fields
// fields: t.U = 1 :: "two" :: true :: HNil

fields("a")  // compile error!

// The compile error is:
//     cmd5.sc:1: No field String("a") in record ammonite.$sess.cmd4.t.U
//     val res5 = fields("a")
//                      ^
//     Compilation Failed


サイドノート:

奇妙なことに、マクロが行うことを手動で行うと、次のように機能します。

// I can't actually instantiate a new `Flattened[_]` manually, since
// defining the type `U` would be convoluted (not even sure it can be
// done), so an object with the same field is the next best thing.
object Foo {
  import shapeless._
  import syntax.singleton._
  import record._

  val fields = ("a" ->> 1) :: ("b" ->> "two") :: ("c" ->> true) :: HNil
}

val a = Foo.fields("a")
// a: Int = 1

val b = Foo.fields("b")
// b: String = "two"

val c = Foo.fields("c")
// c: Boolean = true

この不一致があるのはなぜですか? また、マクロ バージョンを手動バージョンと同じように動作させるにはどうすればよいですか?

4

1 に答える 1