Почему это не то же самое? - PullRequest
3 голосов
/ 29 мая 2019

Я новичок в 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) в вызов func.

Получение ошибки:

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 Ответ

3 голосов
/ 29 мая 2019

Это из-за макросов, написанных разработчиками 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)
}

1010 * производит *

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`)));

вы можете видеть r, s вне их видимости.

Так что дело в том, что в макросах Accord есть ошибки. Они не ожидают кода, подобного второму, но ожидают кода, подобного первому.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...