0

我正在尝试在其类型参数中组合一些具有复合类型的函数:

trait Contains[T]
trait Sentence
trait Token
def sentenceSegmenter[T] = (c: Contains[T]) => null: Contains[T with Sentence]
def tokenizer[T <: Sentence] = (c: Contains[T]) => null: Contains[T with Token]

我的主要目标是能够用以下简单的东西来组合它们:

val pipeline = sentenceSegmenter andThen tokenizer

但是,这会产生编译错误,因为 Scala 推断需要的类型tokenizerContains[? with Sentence] => ?

scala> val pipeline = sentenceSegmenter andThen tokenizer
<console>:12: error: polymorphic expression cannot be instantiated to expected type;
 found   : [T <: Sentence]Contains[T] => Contains[T with Token]
 required: Contains[? with Sentence] => ?

       val pipeline = sentenceSegmenter andThen tokenizer
                                                ^

我尝试了一个稍微不同的定义,tokenizer它更接近 Scala 推断的类型,但我得到了类似的错误:

scala> def tokenizer[T] = (c: Contains[T with Sentence]) => null: Contains[T with Sentence with Token]
tokenizer: [T]=> Contains[T with Sentence] => Contains[T with Sentence with Token]

scala> val pipeline = sentenceSegmenter andThen tokenizer
<console>:12: error: polymorphic expression cannot be instantiated to expected type;
 found   : [T]Contains[T with Sentence] => Contains[T with Sentence with Token]
 required: Contains[? with Sentence] => ?

       val pipeline = sentenceSegmenter andThen tokenizer
                                                ^

如果我指定几乎任何类型以及sentenceSegmenter,或者如果我创建一个没有类型参数的虚假初始函数,我可以编译一些东西:

scala> val pipeline = sentenceSegmenter[Nothing] andThen tokenizer
pipeline: Contains[Nothing] => Contains[Nothing with Sentence with Sentence with Token] = <function1>

scala> val pipeline = sentenceSegmenter[Any] andThen tokenizer
pipeline: Contains[Any] => Contains[Any with Sentence with Sentence with Token] = <function1>

scala> val begin = identity[Contains[Any]] _
begin: Contains[Any] => Contains[Any] = <function1>

scala> val pipeline = begin andThen sentenceSegmenter andThen tokenizer
pipeline: Contains[Any] => Contains[Any with Sentence with Sentence with Token] = <function1>

我不介意类型Any或被Nothing推断,因为我真的不在乎是什么T。(我主要关心这with XXX部分。)但我希望它被推断出来,而不是必须明确指定它,或者通过虚假的初始函数提供它。

4

1 回答 1

1

您不能绑定 ( val) 类型参数。您必须改用 a def,以便它可以在使用时绑定类型:

  def pipeline[T] = sentenceSegmenter[T] andThen tokenizer

请注意,您可以使用推断类型调用管道:

scala> new Contains[Sentence] {}
res1: Contains[Sentence] = $anon$1@5aea1d29

scala> pipeline(res1)
res2: Contains[Sentence with Sentence with Token] = null
于 2013-07-11T19:40:04.597 回答