如何判断一个函数的输出类型(type)? [英] How to determine the output type of a function (type)?

查看:20
本文介绍了如何判断一个函数的输出类型(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 => 来为此创建一个通用类型.UnitReader =>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.

这篇关于如何判断一个函数的输出类型(type)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆