为什么不一样?

时间:2019-05-29 14:01:11

标签: scala validation

我是Scala的新手,我有两段代码:

private val uploadImageTypeValidator = validator[(UploadImageRequest, DriverSession)] { case (r, s) =>
    val sessionType = SessionType.fromValue(s.`type`)
    r.`type` is sessionImageTypeValidators(sessionType)
  }

 private val uploadImageTypeValidator = validator[(UploadImageRequest, DriverSession)] { case (r, s) =>
    r.`type` is sessionImageTypeValidators(SessionType.fromValue(s.`type`))
  }

我刚刚将SessionType.fromValue(s.type)复制到函数调用中。

获取错误:

Error:(53, 5) not found: value r
    r.`type` is sessionImageTypeValidators(SessionType.fromValue(s.`type`))
Error:(53, 66) not found: value s
    r.`type` is sessionImageTypeValidators(SessionType.fromValue(s.`type`))

第二个代码为什么不编译?仅适用于val

1 个答案:

答案 0 :(得分:3)

这是因为Accord验证库的开发人员编写了宏。

在第一scalacOptions += "-Ymacro-debug-lite"中使用build.sbt

private val uploadImageTypeValidator0 = validator[(UploadImageRequest, DriverSession)] { case (r, s) =>
  val sessionType = SessionType.fromValue(s.`type`)
  r.`type` is sessionImageTypeValidators(sessionType)
}

产生

Warning:scalac: performing macro expansion com.wix.accord.dsl.`package`.validator[(App.UploadImageRequest, App.DriverSession)](((x0$1: (App.UploadImageRequest, App.DriverSession)) => x0$1 match {
  case (_1: App.UploadImageRequest, _2: App.DriverSession)(App.UploadImageRequest, App.DriverSession)((r @ _), (s @ _)) => {
    val sessionType: App.SessionType = App.this.SessionType.fromValue(s.`type`);
    {
      com.wix.accord.dsl.`package`.Contextualizer[String](r.`type`).is(App.this.sessionImageTypeValidators(sessionType));
      ()
    }
  }
})) at source-/media/data/Projects/accord_validation_demo/src/main/scala/App.scala,line-6,offset=164
Warning:scalac: 
Warning:scalac: new com.wix.accord.transform.ValidationTransform.TransformedValidator(new com.wix.accord.combinators.Conditional[(App.UploadImageRequest, App.DriverSession)](Seq(), None))
Apply(Select(New(Select(Select(Select(Select(Select(Ident(TermName("com")), TermName("wix")), TermName("accord")), TermName("transform")), TermName("ValidationTransform")), TypeName("TransformedValidator"))), termNames.CONSTRUCTOR), List(Apply(Select(New(AppliedTypeTree(Select(Select(Select(Select(Ident(TermName("com")), TermName("wix")), TermName("accord")), TermName("combinators")), TypeName("Conditional")), List(TypeTree()))), termNames.CONSTRUCTOR), List(Apply(Ident(TermName("Seq")), List()), Ident(TermName("None"))))))

但是第二段代码

private val uploadImageTypeValidator = validator[(UploadImageRequest, DriverSession)] { case (r, s) =>
  r.`type` is sessionImageTypeValidators(SessionType.fromValue(s.`type`))
}

产生

Warning:scalac: performing macro expansion com.wix.accord.dsl.`package`.validator[(App.UploadImageRequest, App.DriverSession)](((x0$1: (App.UploadImageRequest, App.DriverSession)) => x0$1 match {
  case (_1: App.UploadImageRequest, _2: App.DriverSession)(App.UploadImageRequest, App.DriverSession)((r @ _), (s @ _)) => {
    com.wix.accord.dsl.`package`.Contextualizer[String](r.`type`).is(App.this.sessionImageTypeValidators(App.this.SessionType.fromValue(s.`type`)));
    ()
  }
})) at source-/media/data/Projects/accord_validation_demo/src/main/scala/App.scala,line-11,offset=392
Warning:scalac: 
Warning:scalac: new com.wix.accord.transform.ValidationTransform.TransformedValidator(new com.wix.accord.combinators.Conditional[(App.UploadImageRequest, App.DriverSession)](Seq(((x0$1: (App.UploadImageRequest, App.DriverSession)) => x0$1 match {
  case (_1: App.UploadImageRequest, _2: App.DriverSession)(App.UploadImageRequest, App.DriverSession)((r @ _), (s @ _)) => true
  case _ => false
}).$minus$greater({
  final class $anon extends com.wix.accord.Validator[(App.UploadImageRequest, App.DriverSession)] {
    def <init>() = {
      super.<init>();
      ()
    };
    import com.wix.accord.DescriptionBuilders._;
    def apply(x0$1: (App.UploadImageRequest, App.DriverSession)) = {
      val validation$0 = com.wix.accord.dsl.`package`.Contextualizer[String](r.`type`).is(App.this.sessionImageTypeValidators(App.this.SessionType.fromValue(s.`type`)));
      val path$0 = {
        <synthetic> <artifact> val x$1 = com.wix.accord.Descriptions.PatternMatch(on = com.wix.accord.Descriptions.Path(), value = x0$1, guard = scala.None);
        com.wix.accord.Descriptions.Path(com.wix.accord.Descriptions.Generic("r.`type`")).$plus$colon(x$1)
      };
      validation$0(r.`type`).prepend(path$0)
    }
  };
  new $anon()
})), None))
Apply(Select(New(Select(Select(Select(Select(Select(Ident(TermName("com")), TermName("wix")), TermName("accord")), TermName("transform")), TermName("ValidationTransform")), TypeName("TransformedValidator"))), termNames.CONSTRUCTOR), List(Apply(Select(New(AppliedTypeTree(Select(Select(Select(Select(Ident(TermName("com")), TermName("wix")), TermName("accord")), TermName("combinators")), TypeName("Conditional")), List(TypeTree()))), termNames.CONSTRUCTOR), List(Apply(Ident(TermName("Seq")), List(Apply(Select(Function(List(ValDef(Modifiers(PARAM | SYNTHETIC), TermName("x0$1"), TypeTree(), EmptyTree)), Match(Ident(TermName("x0$1")), List(CaseDef(Apply(TypeTree().setOriginal(Select(Ident(scala), scala.Tuple2)), List(Bind(TermName("r"), Ident(termNames.WILDCARD)), Bind(TermName("s"), Ident(termNames.WILDCARD)))), EmptyTree, Literal(Constant(true))), CaseDef(Ident(termNames.WILDCARD), EmptyTree, Literal(Constant(false)))))), TermName("$minus$greater")), List(Block(List(ClassDef(Modifiers(FINAL), TypeName("$anon"), List(), Template(List(AppliedTypeTree(Select(Select(Select(Ident(TermName("com")), TermName("wix")), TermName("accord")), TypeName("Validator")), List(TypeTree()))), noSelfType, List(DefDef(Modifiers(), termNames.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List(pendingSuperCall), Literal(Constant(())))), Import(Select(Select(Select(Ident(TermName("com")), TermName("wix")), TermName("accord")), TermName("DescriptionBuilders")), List(ImportSelector(termNames.WILDCARD, -1, null, -1))), DefDef(Modifiers(), TermName("apply"), List(), List(List(ValDef(Modifiers(PARAM | SYNTHETIC), TermName("x0$1"), TypeTree(), EmptyTree))), TypeTree(), Block(List(ValDef(Modifiers(), TermName("validation$0"), TypeTree(), Apply(Select(Apply(TypeApply(Select(Select(Select(Select(Select(Ident(TermName("com")), TermName("wix")), TermName("accord")), TermName("dsl")), termNames.PACKAGE), TermName("Contextualizer")), List(TypeTree())), List(Select(Ident(TermName("r")), TermName("type")))), TermName("is")), List(Apply(Select(This(TypeName("App")), TermName("sessionImageTypeValidators")), List(Apply(Select(Select(This(TypeName("App")), TermName("SessionType")), TermName("fromValue")), List(Select(Ident(TermName("s")), TermName("type"))))))))), ValDef(Modifiers(), TermName("path$0"), TypeTree(), Block(List(ValDef(Modifiers(SYNTHETIC | ARTIFACT), TermName("x$1"), TypeTree(), Apply(Select(Select(Select(Select(Ident(TermName("com")), TermName("wix")), TermName("accord")), TermName("Descriptions")), TermName("PatternMatch")), List(AssignOrNamedArg(Ident(TermName("on")), Apply(Select(Select(Select(Select(Ident(TermName("com")), TermName("wix")), TermName("accord")), TermName("Descriptions")), TermName("Path")), List())), AssignOrNamedArg(Ident(TermName("value")), Ident(TermName("x0$1"))), AssignOrNamedArg(Ident(TermName("guard")), Select(Ident(TermName("scala")), TermName("None"))))))), Apply(Select(Apply(Select(Select(Select(Select(Ident(TermName("com")), TermName("wix")), TermName("accord")), TermName("Descriptions")), TermName("Path")), List(Apply(Select(Select(Select(Select(Ident(TermName("com")), TermName("wix")), TermName("accord")), TermName("Descriptions")), TermName("Generic")), List(Literal(Constant("r.`type`")))))), TermName("$plus$colon")), List(Ident(TermName("x$1"))))))), Apply(Select(Apply(Ident(TermName("validation$0")), List(Select(Ident(TermName("r")), TermName("type")))), TermName("prepend")), List(Ident(TermName("path$0")))))))))), Apply(Select(New(Ident(TypeName("$anon"))), termNames.CONSTRUCTOR), List())))))), Ident(TermName("None"))))))

这里

val validation$0 = com.wix.accord.dsl.Contextualizer[String](r.`type`).is(App.this.sessionImageTypeValidators(App.this.SessionType.fromValue(s.`type`)));

您会看到rs不在其范围内。

所以,事实是Accord宏存在错误。他们不期望像第二部分那样的代码,但是期望像第一部分那样的代码。