【发布时间】:2013-07-12 04:38:45
【问题描述】:
我正在尝试在其类型参数中组合一些具有复合类型的函数:
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 部分。)但我希望它被推断出来,而不是必须明确指定它,或者通过虚假的初始函数提供它。
【问题讨论】:
标签: scala types function-composition