9

dotty でメタプログラミングを学ぼうとしています。具体的には、コンパイル時のコード生成。何かを作って学ぶのは良いアプローチだと思いました。そこで、行をケース クラスに解析する CSV パーサーを作成することにしました。dotty マクロを使用してデコーダーを生成したい

trait Decoder[T]{
  def decode(str:String):Either[ParseError, T]
}

object Decoder {
  inline given stringDec as Decoder[String] = new Decoder[String] {
    override def decode(str: String): Either[ParseError, String] = Right(str)
  }

  inline given intDec as Decoder[Int] = new Decoder[Int] {
    override def decode(str: String): Either[ParseError, Int] =
      str.toIntOption.toRight(ParseError(str, "value is not valid Int"))
  }
  
  inline def forType[T]:Decoder[T] = ${make[T]}

  def make[T:Type](using qctx: QuoteContext):Expr[Decoder[T]] = ???
}

Int&の基本的なデコーダーを提供しましたが、メソッドStringのガイダンスを探しています。def make[T:Type]このメソッド内でケース クラスのパラメータ リストを反復する方法はT? これを行うための推奨される方法やパターンはありますか?

4

1 に答える 1