gpt4 book ai didi

scala - 找到 scala.Unit,需要 Unit

转载 作者:行者123 更新时间:2023-12-04 02:11:37 27 4
gpt4 key购买 nike

我有一个像这样的简单容器特征:

trait Handler {
def apply[In, Out](in: In): Out
}

当我尝试实现它时:

new Handler {
def apply[Any, Unit](in: Any) = println(in)
}

我得到一个错误:

[error]  found   : scala.Unit
[error] required: Unit(in method apply)
[error] def apply[Any, Unit](in: Any) = println(in)

这是为什么?我可以修复这个编译器错误吗?

最佳答案

正确的answer几乎是一样的问题.

你可以使用特征类型参数,而不是方法类型参数:

trait Handler[Out] {
def apply[In](in: In): Out
}

new Handler[Unit] {
def apply[In](in: In) = println(in)
}

关于scala - 找到 scala.Unit,需要 Unit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16667627/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com