小编mir*_*lon的帖子

带有varargs的case类的隐式jsonFormat

我有一个包含varargs的case类,隐含的jsonFormat如下:

import spray.json._
case class Colors(name: String*)
object MyJsonProtocol extends DefaultJsonProtocol {
  implicit val colorFormat = jsonFormat1(Colors)
}
import MyJsonProtocol._
Colors("CadetBlue").toJson
Run Code Online (Sandbox Code Playgroud)

它引发了一个错误:

error: type mismatch;
found   : Color2.type
required: Seq[String] => Color2
Note: implicit value colorFormat is not applicable here because it comes after the application point and it lacks an explicit result type
      implicit val colorFormat = jsonFormat1(Color2)
                                            ^
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

implicit val colorFormat = jsonFormat1(Colors.apply)
Run Code Online (Sandbox Code Playgroud)

这导致了一个不同的(运行时!)异常:

java.lang.RuntimeException: Cannot automatically determine case class field names and order for 'Colors', please …
Run Code Online (Sandbox Code Playgroud)

scala akka spray spray-json

5
推荐指数
2
解决办法
3557
查看次数

在类构造函数中使用trait方法

我有一个特质和一个扩展特性的类.我可以使用特征中的方法如下:

trait A {
  def a = ""
}

class B(s: String) extends A {
  def b = a 
}
Run Code Online (Sandbox Code Playgroud)

但是,当我在构造函数中使用trait的方法时,如下所示:

trait A {
  def a = ""
}

class B(s: String) extends A {
  def this() = this(a) 
}
Run Code Online (Sandbox Code Playgroud)

然后出现以下错误:

error: not found: value a
Run Code Online (Sandbox Code Playgroud)

有没有办法为特征中的类构造定义默认参数?

编辑:澄清目的:有akka-testkit:

class TestKit(_system: ActorSystem) extends { implicit val system = _system }
Run Code Online (Sandbox Code Playgroud)

每个测试看起来像这样:

class B(_system: ActorSystem) extends TestKit(_system) with A with ... {
  def this() = this(actorSystem)
  ...
}
Run Code Online (Sandbox Code Playgroud)

因为我想在A中创建ActorSystem的通用创建:

trait A …
Run Code Online (Sandbox Code Playgroud)

constructor scala class traits

5
推荐指数
1
解决办法
87
查看次数

没有名为“ termcolor”的模块

没有名为“ termcolor”的模块

pip install termcolor 退货

C:\Users\admin>pip install termcolor
Requirement already satisfied: termcolor in c:\python27\lib\site-packages
You are using pip version 9.0.1, however version 10.0.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
Run Code Online (Sandbox Code Playgroud)

不确定为什么我不能导入此模块?这是代码。

from termcolor import colored
Run Code Online (Sandbox Code Playgroud)

python

5
推荐指数
2
解决办法
8530
查看次数

SBT 0.13.7和Scala 2.11.5版本兼容性问题

尝试使用scala版本2.11.5的sbt版本0.13.7.两者都是最新的.他们一起工作吗?当我跑sbt clean compile,它打印:

[info] 'compiler-interface' not yet compiled for Scala 2.11.5. Compiling...
error: java.lang.NoClassDefFoundError: scala/tools/nsc/typechecker/Infer$Inferencer
Run Code Online (Sandbox Code Playgroud)

当更改为scala 2.11.4时,一切正常:

[info] 'compiler-interface' not yet compiled for Scala 2.11.4. Compiling...
[info]   Compilation completed in 13.434 s
Run Code Online (Sandbox Code Playgroud)

我找不到有关sbt和scala版本兼容性的任何相关来源.它是否与sbt构建针对scala 2.11有关?

scala compiler-errors compilation sbt scala-2.11

3
推荐指数
1
解决办法
1912
查看次数

在 pureconfig 中表示任一

我有一个这样的 HOCON 配置:

[
    {
        name = 1
        url = "http://example.com"
    },
    {
        name = 2
        url = "http://example2.com"
    },
    {
        name = 3
        url = {
            A = "http://example3.com"
            B = "http://example4.com"
        }
    }
]
Run Code Online (Sandbox Code Playgroud)

我想用 pureconfig 解析它。我如何表示 URL 可以是字符串或多个 URL 的映射,每个 URL 都有一个键?

我试过这个:

import pureconfig.ConfigSource
import pureconfig.generic.auto.exportReader

case class Site(name: Int, url: Either[String, Map[String, String]])
case class Config(sites: List[Site])
ConfigSource.default.loadOrThrow[Config]
Run Code Online (Sandbox Code Playgroud)

但结果是“预期类型为 OBJECT。改为找到 STRING”。

我知道 pureconfig 支持Option. 我发现没有提到支持Either,这是否意味着它可以用其他东西代替?

scala config either hocon pureconfig

2
推荐指数
1
解决办法
493
查看次数