【发布时间】:2021-04-22 14:49:35
【问题描述】:
我是 Scala 新手,遇到了这种奇怪的情况。
def bar[A, B](implicit foo: A => B): B = {
// do something
foo
}
然后我得到了类似的错误
require B but found A => B
我应该如何从 A 中得到 B => B
这就是我这样做的原因,我有两个功能:
def funcA: String = {
def getStrA: String = "A"
// then there's the same operation in both functions
Try{ } match {
case Success(_) => getStrA
case Failure(_) => // exactlly same error handler in both function
}
}
def funcB: Int = {
def doSomething(x: Int): Int = {
// do something
x / 1
}
val x = 1
Try{ } match {
case Success(_) => doSomething(1)
case Failure(_) => // exactlly same error handler in both function
}
}
这就是我想要实现的目标
def funcA: String = {
implicit def getStrA: String = "A"
bar
}
def funcB: Int = {
val x = 1
implicit def doSomething(x: Int): Int = {
// do something
x / 1
}
bar
}
def bar[A, B](implicit foo: A => B): B = {
Try{ } match {
case Success(_) => foo
case Failure(_) => // exactlly same error handler in both function
}
}
【问题讨论】:
-
调用
foo()传递A类型的值。 -
那我应该怎么称呼这个
bar函数 -
This 可能对你有帮助
标签: scala functional-programming