我正在尝试在其类型参数中组合一些具有复合类型的函数:
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 推断需要的类型tokenizer
为Contains[? 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
部分。)但我希望它被推断出来,而不是必须明确指定它,或者通过虚假的初始函数提供它。