如何定义与泛型参数关联的函数参数默认值? [英] How do I define a function parameter default associated with a generic parameter?

查看:291
本文介绍了如何定义与泛型参数关联的函数参数默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图重构一个函数(在这个StackOverflow答案的末尾找到),使它稍微更通用。这里是原始的函数定义:

pre $ def $ {code> def tryProcessSource(
file:File,
parseLine:(Int,字符串)=>选项[列表[字符串]] =
(index,unparsedLine)=>一些(列表(unparsedLine)),
filterLine:(Int,List [String])=> Option [Boolean] =
(index,parsedValues)=>某些(true),
retainValues:(Int,List [String])=> Option [List [String]] =
(index,parsedValues)=> Some(parsedValues)
):Try [List [List [String]]] = {
???
}

这里是我试图改变它的地方:

  def tryProcessSource [A< ;: Any](
file:File,
parseLine:(Int,String)= > Option [List [String]] =
(index,unparsedLine)=> Some(List(unparsedLine)),
filterLine:(Int,List [String])=> Option [Boolean ] =
(index,parsedValues)=>一些(真),
transformLine:(Int,List [String])=> Option [A] =
(index,parsedValues) => Some(parsedValues)
):Try [List [A]] = {
???

我一直在IntelliJ编辑器中出现 Some(parsedValues),它表示Some [List [String]]类型的表达式不符合预期类型Option [A]。我一直无法弄清楚如何正确修改函数定义来满足所需的条件;如果我将 transformLine 更改为此(替换通用参数 A with 任何)...

  transformLine:(Int,List [String])=>选项[Any] = 
(index,parsedValues)=>一些(parsedValues)

...错误消失。但是,我也失去了与泛型参数相关的强类型。



任何有关这方面的帮助都非常感谢。

>解决方案在 transformLine:(Int,List [String])=>选项[A] =(index,parsedValues)=>无论 parsedValues 显然有类型 List [String] ,所以 Some(parsedValues)某些[List [String]] 。对于 transformLine ,根本没有合理的默认值。

您可以将其类型更改为(Int,A)=>选项[A] (Int,List [A])=>选项[List [A]] 取决于你需要什么,并且改变以前的参数,但是你会被卡住的事实是行是 String s,因此您必须将它们转换为 A 某处


I am attempting to refactor a function (found towards the end of this StackOverflow answer) to make it slightly more generic. Here's the original function definition:

def tryProcessSource(
  file: File,
  parseLine: (Int, String) => Option[List[String]] =
    (index, unparsedLine) => Some(List(unparsedLine)),
  filterLine: (Int, List[String]) => Option[Boolean] =
    (index, parsedValues) => Some(true),
  retainValues: (Int, List[String]) => Option[List[String]] =
    (index, parsedValues) => Some(parsedValues)
): Try[List[List[String]]] = {
  ???
}

And here is to what I am attempting to change it:

def tryProcessSource[A <: Any](
  file: File,
  parseLine: (Int, String) => Option[List[String]] =
    (index, unparsedLine) => Some(List(unparsedLine)),
  filterLine: (Int, List[String]) => Option[Boolean] =
    (index, parsedValues) => Some(true),
  transformLine: (Int, List[String]) => Option[A] =
    (index, parsedValues) => Some(parsedValues)
): Try[List[A]] = {
  ???
}

I keep getting a highlight error in the IntelliJ editor on Some(parsedValues) which says, "Expression of type Some[List[String]] doesn't conform to expected type Option[A]". I've been unable to figure out how to properly modify the function definition to satisfy the required condition; i.e. so the error goes away.

If I change transformLine to this (replace the generic parameter A with Any)...

transformLine: (Int, List[String]) => Option[Any] =
  (index, parsedValues) => Some(parsedValues)

...the error goes away. But, I also lose the strong typing associated with the generic parameter.

Any assistance on with this is very much appreciated.

解决方案

In transformLine: (Int, List[String]) => Option[A] = (index, parsedValues) => whatever, parsedValues obviously has type List[String], and so Some(parsedValues) is Some[List[String]]. There is simply no reasonable default value for transformLine.

You can change its type to (Int, A) => Option[A] or (Int, List[A]) => Option[List[A]] depending on what you need, and change the previous arguments as well, but you'll get stuck on the fact that lines are Strings, so you'll have to convert them to A somewhere.

这篇关于如何定义与泛型参数关联的函数参数默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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