Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
scala中函数组合顺序的编译时约束_Scala_Types_Function Composition - Fatal编程技术网

scala中函数组合顺序的编译时约束

scala中函数组合顺序的编译时约束,scala,types,function-composition,Scala,Types,Function Composition,我想定义一些处理自然语言文本的函数。这些函数中的每一个都会在文本中添加一些“注释”,例如: class Annotation(val begin: Int, val end: Int) class Sentence(begin: Int, end: Int) extends Annotation(begin, end) class Token(begin: Int, end: Int) extends Annotation(begin, end) 因此,我可能有一个添加标记注释的标记器函数,一

我想定义一些处理自然语言文本的函数。这些函数中的每一个都会在文本中添加一些“注释”,例如:

class Annotation(val begin: Int, val end: Int)
class Sentence(begin: Int, end: Int) extends Annotation(begin, end)
class Token(begin: Int, end: Int) extends Annotation(begin, end)
因此,我可能有一个添加标记注释的标记器函数,一个添加句子注释的句子分段器函数,等等。这些函数对它们的运行顺序有一些限制。例如,标记器可能需要句子注释,因此必须在句子分段器之后运行。在这种情况下,如果我不小心按错误的顺序编写了这些函数,我希望得到一个编译错误。因此,
sentenceSegmenter和第二个标记器
应该编译,但
标记器和第二个sentenceSegmenter
不应该编译

下面是我的尝试。我为文本定义了一个特殊的容器类型,其中类型参数指定(通过复合类型)添加到文本中的注释,然后函数适当地指定其类型参数,以确保在其先决条件成为复合类型的一部分之前无法运行它们

trait AnalyzedText[T] {
  def text: String
  def ++[U](annotations: Iterator[U]): AnalyzedText[T with U] 
}

val begin: (AnalyzedText[Any] => AnalyzedText[Any]) = identity
def sentenceSegmenter[T]: (AnalyzedText[T] => AnalyzedText[T with Sentence]) = ???
def tokenizer[T <: Sentence]: (AnalyzedText[T] => AnalyzedText[T with Token]) = ???

// compiles
val pipeline = begin andThen sentenceSegmenter andThen tokenizer
// fails to compile -- good!
//val brokenPipeline = begin andThen tokenizer andThen sentenceSegmenter
但是Scala编译器不知道如何推断
++
方法的类型参数,除非我手动指定类型参数
text.++[Token](…)
,否则会产生错误:

type mismatch;  found: Iterator[Token]  required: Iterator[Nothing]
有没有办法得到要推断的类型参数?或者,我对这个问题的想法是错的吗?有没有更好的方法在Scala中捕获这些类型的函数组合约束?

看起来。同时还有一个非常简单的解决方法,只需将处理器定义为一个方法,并省略返回类型:

def tokenizer[T <: Sentence](text: AnalyzedText[T]) =
  text ++ "\\S+".r.findAllMatchIn(text.text).map(m => new Token(m.start, m.end))

我看了一眼,但没有发现任何明显相关的东西。如果你有时间的话,也许值得再多挖掘一些,提出一个问题或发电子邮件。

。谢谢!是的,你的变通方法似乎很好地解决了我的问题。有趣的是,显式声明类型是问题的根源。。。我确实提出了一个问题()。我们将看看这是一个bug还是一个特性
def tokenizer[T <: Sentence](text: AnalyzedText[T]) =
  text ++ "\\S+".r.findAllMatchIn(text.text).map(m => new Token(m.start, m.end))
def tokFunc[T <: Sentence]: (AnalyzedText[T] => AnalyzedText[T with Token]) =
  tokenizer _