Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
scala:库正在寻找要声明的隐式值_Scala_Scope_Implicit - Fatal编程技术网

scala:库正在寻找要声明的隐式值

scala:库正在寻找要声明的隐式值,scala,scope,implicit,Scala,Scope,Implicit,我使用两种进口产品 import org.json4s._ import org.json4s.native.JsonMethods._ 我有以下源代码 val json = parse("~~~~~~aklsdjfalksdjfalkdsf") var abc = (json \\ "something").children map { _.extract[POJO] } 我跑完后看到 Error:(32, 18) No org.json4s.Formats fou

我使用两种进口产品

import org.json4s._
import org.json4s.native.JsonMethods._
我有以下源代码

val json = parse("~~~~~~aklsdjfalksdjfalkdsf")
var abc = (json \\ "something").children map {
        _.extract[POJO]
    }
我跑完后看到

Error:(32, 18) No org.json4s.Formats found. Try to bring an instance of org.json4s.Formats in scope or use the org.json4s.DefaultFormats.
        _.extract[POJO]

Error:(32, 18) not enough arguments for method extract: (implicit formats: org.json4s.Formats, implicit mf: scala.reflect.Manifest[POJO])POJO.
Unspecified value parameters formats, mf.
        _.extract[POJO]
我知道我应该声明:

 implicit val df = DefaultFormats
我学会了如何在scala代码中使用“隐式”。 但是,我需要了解如何使用一个库,强制开发人员在源代码中定义隐式变量

正如错误消息中所述,ExtractableJsonAstNode类文件中的“extract”方法中似乎使用了关键字“implicit”

 def extract[A](implicit formats: Formats, mf: scala.reflect.Manifest[A]): A =
    Extraction.extract(jv)(formats, mf)
我看到这是在我的源代码中寻找“隐式”变量关键字

第一个问题是我如何知道隐式关键字何时用于另一个隐式关键字(例如,在库中声明),它将是我定义的操作的切换(“隐式”不被声明两次的情况) 我唯一的线索是,当源代码母代码使用'implicit'关键字并使用变量时,它的类型是一种特征。然后,我(dev)需要声明一个变量,该变量的类型为扩展该特性的具体类。我不知道这是不是真的

我还在json库中的“Formats.scala”文件中找到了以下源代码

 class CustomSerializer[A: Manifest](
    ser: Formats => (PartialFunction[JValue, A], PartialFunction[Any, JValue])) extends Serializer[A] {

    val Class = implicitly[Manifest[A]].runtimeClass

    def deserialize(implicit format: Formats) = {
      case (TypeInfo(Class, _), json) =>
        if (ser(format)._1.isDefinedAt(json)) ser(format)._1(json)
        else throw new MappingException("Can't convert " + json + " to " + Class)
    }

    def serialize(implicit format: Formats) = ser(format)._2
  }
请注意,
def反序列化(隐式格式:格式)
已声明。 一旦我在文件中写入'implicit val df=DefaultFormats',它会影响整个json机制吗?不仅仅是'extract'方法吗?因为json库中使用了CustomSerializer

总结。。 第一个问题是关于“隐式”关键字用法之一。 第二个问题是关于“隐式”关键字范围。

何时使用隐式关键字

隐式用于定义您通常无法控制的行为。在您的问题中,
DefaultFormats
已经是一个隐式格式。您不需要使用它声明新的隐式,只需导入它即可

至于知道您正在使用的库何时需要一些隐含的作用域,就是这个错误。它本质上是告诉你“如果你不确定这个错误是关于什么的,你可以直接导入
DefaultFormats

一个隐含的机制会影响整个机制吗

这是一个需要理解的关键问题

当您有一个接受隐式的函数时,编译器将在作用域中搜索该类型的隐式函数

您的函数正在查找
org.json4s.Formats
。通过导入
DefaultFormat
或编写自己的隐式类型
Format
,您可以调用您的函数来使用该格式

这对代码的其余部分有什么影响

在作用域中依赖隐式
格式的任何其他函数都将使用相同的隐式格式。这可能对您很合适


如果需要使用多个不同的格式,则需要将这些组件彼此分离。您不希望在同一范围内定义同一类型的多个隐式。这会让人和计算机感到困惑,应该避免使用。

看起来您可以将org.json4s.DefaultFormats导入到您要导入的文件中正在使用提取方法。它应该已经是隐式的。