如何判断一个函数的输出类型(type)?
我们使用的 IO Monad 如下所示:
We're using an IO Monad which looks like this:
sealed trait IOCompnent {
def writer: AbstractWriter
def reader: AbstractReader
trait AbstractWriter {
def writeFile(parentFolder: String, fileName: String)(f: Writer => Unit): Try[Unit]
}
trait AbstractReader {
def readFile(filePath: String, fileName: String)(f: Reader => String): Try[String]
}
}
由于我们也有一个类似的组件,它使用 InputStream
/OutputStream
,我想通过提取 Writer => 来为此创建一个通用类型.Unit
和 Reader =>String
转换成一个类型(因为 InputStream
的结果是 Array[Byte]
而不是 String
).
Since we also have a similar component which uses InputStream
/OutputStream
, I would like to create a generic type for this by extracting the Writer => Unit
and Reader => String
into a type (because the result of the InputStream
is Array[Byte]
instead of String
).
唯一的问题是,readFile
方法返回读取函数结果的 Try
,所以如果我定义了 readFile
函数如下(只读取IOComponent
的部分),如何判断Try
的类型?
The only problem is, is that the readFile
method returns a Try
of the result of the read function, so if I define the readFile
function as follows (only the read part of the IOComponent
), how to determine the type of the Try
?
sealed trait ReadComponent[READER] {
def reader: AbstractReader
trait AbstractReader {
def readFile(filePath: String, fileName: String)(f: READER): Try[???]
}
}
我最喜欢的选项是 def readFile[T](filePath: String, fileName: String)(f: READER => T): Try[T]
让 f 决定输出类型.
Let f decide of is output type.