加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 如何使用无形来检测字段类型注释

发布时间:2020-12-16 18:34:56 所属栏目:安全 来源:网络整理
导读:我正在尝试使用无形的方法收集在编译时具有特定注释的案例类的字段.我试图玩下面的片段,但它没有按预期工作(输出什么,而不是打
我正在尝试使用无形的方法收集在编译时具有特定注释的案例类的字段.我试图玩下面的片段,但它没有按预期工作(输出什么,而不是打印“我”).我怎样才能使它工作?

import shapeless._
import shapeless.labelled._

final class searchable() extends scala.annotation.StaticAnnotation
final case class Foo(@searchable i: Int,s: String)

trait Boo[A] {
  def print(a: A): Unit
}
sealed trait Boo0 {
  implicit def hnil = new Boo[HNil] { def print(hnil: HNil): Unit = () }
  implicit def hlist[K <: Symbol,V,RL <: HList](implicit b: Boo[RL]): Boo[FieldType[K,V] :: RL] =
    new Boo[FieldType[K,V] :: RL] {
      def print(a: FieldType[K,V] :: RL): Unit = {
        b.print(a.tail)
      }
    }
}
sealed trait Boo1 extends Boo0 {
  implicit def hlist1[K <: Symbol,RL <: HList](implicit annot: Annotation[searchable,K],witness: Witness.Aux[K],b: Boo[RL]): Boo[FieldType[K,V] :: RL): Unit = {
        Console.println(witness.value.name)
        b.print(a.tail)
      }
    }
}
object Boo extends Boo1 {
  implicit def generics[A,HL <: HList](implicit iso: LabelledGeneric.Aux[A,HL],boo: Boo[HL]): Boo[A] =
    new Boo[A] {
      def print(a: A): Unit = {
        boo.print(iso.to(a))
      }
    }
}

implicitly[Boo[Foo]].print(Foo(1,"2"))

解决方法

查看Annotation的宏,它会直接拒绝不是产品或副产品的类型

val annTreeOpts =
  if (isProduct(tpe)) { ... }
  else if (isCoproduct(tpe)) { ... }
  else abort(s"$tpe is not case class like or the root of a sealed family of types")

这是非常不幸的,因为在每个字段符号级别收集类型注释有时可能非常有用.

在同一文件中定义了另一个类型类Annotations,它可以将字段上的特定注释实际收集到HList中.然而问题是现场信息完全丢失.有一种笨拙的方式来破解一起来为我的用例服务……

// A is our annotation
// B is our result type    
// C is our case class with some fields annotated with A

def empty: B = ???
def concat(b1: B,b2: B): B = ???
def func(a: A,nm: String): B = ???

object Collector extends Poly2 {
   implicit def some[K <: Symbol](implicit witness: Witness.Aux[K]) = 
     at[B,(K,Some[A])] { case (b,(_,a)) => concat(b,func(a.get,witness.value.name)) }
   implicit def none[K <: Symbol] = at[B,None.type)] { case (b,_) => b }
}

def collect[HL <: HList,RL <: HList,KL <: HList,ZL <: HList](implicit 
    iso: LabelledGeneric.Aux[C,annot: Annotations.Aux[A,C,RL],keys: Keys.Aux[HL,KL],zip: Zip.Aux[KL :: RL :: HNil,ZL],leftFolder: LeftFolder.Aux[ZL,B,Collector.type,B]): B = {
  zip(keys() :: annot() :: HNil).foldLeft(empty)(Collector)
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读