我有一个类似的课程
case class A(a: Int, b: String)
Run Code Online (Sandbox Code Playgroud)
和一个功能
def f(a: Int)(implicit b: String) =???
Run Code Online (Sandbox Code Playgroud)
可以这样做吗?
val a = A(11, "hello")
a match {
case A(a, implicit b) => f(a)
}
Run Code Online (Sandbox Code Playgroud)
如何在不提取后明确声明参数的情况下隐藏参数b.
我有以下代码.
import scala.collection.mutable.MutableList
val x = MutableList[Int]()
(1 to 10).foreach(x+=1)
Run Code Online (Sandbox Code Playgroud)
我收到了java.lang.IndexOutOfBoundsException: 1错误.
但,
(1 to 10).foreach(println) this does not throw any error.
Run Code Online (Sandbox Code Playgroud)
在indexOutOfBoundException可以通过使用lambda操作如下解决:
(1 to 10).foreach(_ => x+=1)
Run Code Online (Sandbox Code Playgroud)
这一切都很好.
我的问题是:
1.为什么我需要在第一种情况下使用lambda运算符而不像第二种情况?
2.为什么编译器抛出IndexOutOfBoundException,我想这不是这个Exception的正确上下文.
我正在使用延迟重启与 Akka Streams的退避阶段功能,这似乎对我不起作用.
我的测试代码是:
object Test {
import akka.stream.scaladsl.{ Flow, RestartFlow, Sink, Source }
import scala.concurrent.duration._
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
implicit val actorSystem = ActorSystem()
implicit val mat = ActorMaterializer()
def main(args: Array[String]): Unit = {
val source = Source(1 to 10)
val flow = Flow[Int].map { x =>
println(s"Processing: $x")
if (x != 6) {
x * 2
} else throw new RuntimeException("Baam!!")
}
val restartFlow = RestartFlow.onFailuresWithBackoff(10.milliseconds, 20.milliseconds, 0.2, 3)(() => flow)
source.via(restartFlow).to(Sink.ignore).run()
}
} …Run Code Online (Sandbox Code Playgroud) 我有一个case class让我们说:
case class Offset(a:String, b:Int, c: UUID) {
override def toString: String = productIterator.mkString(",")
}
val offset: String = Offset("some_String", 2, java.util.UUID.randomUUID).toString
Run Code Online (Sandbox Code Playgroud)
override toString对于case类的方法是否合适,或者我应该实现一个不同的方法让我们说generateOffset同样的事情:
case class Offset(a:String, b:Int, c: UUID) {
def generateOffset: String = productIterator.mkString(",")
}
val offset: String = Offset("some_String", 2, java.util.UUID.randomUUID).generateOffset
Run Code Online (Sandbox Code Playgroud)