我有一个包含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) 我有一个特质和一个扩展特性的类.我可以使用特征中的方法如下:
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) 没有名为“ 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) 尝试使用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有关??
我有一个这样的 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 ×4
akka ×1
class ×1
compilation ×1
config ×1
constructor ×1
either ×1
hocon ×1
pureconfig ×1
python ×1
sbt ×1
scala-2.11 ×1
spray ×1
spray-json ×1
traits ×1