为什么 Scala 不能推断没有括号和点的方法类型

Why Scala can't inferred type of method without parentheses and dots

我写了简单的代码,returns 字符的不可变映射及其作为向量的索引:

def indexes(string: String): Map[Char, Vector[Int]] = (0 until string.length).
  foldLeft(Map[Char, Vector[Int]]()){
    (m, i) => m + (string(i) -> m.getOrElse(string(i), Vector()).:+(i))
  }

例如:

println(indexes("Mississippi"))
// Map(M -> Vector(0), i -> Vector(1, 4, 7, 10), s -> Vector(2, 3, 5, 6), p -> Vector(8, 9))

为什么 Scala 不能推断 m.getOrElse(string(i), Vector()) :+ iVector[Int] 并编译它?我应该改为 m.getOrElse(string(i), Vector()).:+(i)

它将与括号中的 Map 值一起正常工作:

def indexes(string: String): Map[Char, Vector[Int]] = (0 until string.length).
  foldLeft(Map[Char, Vector[Int]]()){
    (m, i) => m + (string(i) -> (m.getOrElse(string(i), Vector()) :+ i))
  }

indexes("Mississippi")
// res1: Map[Char,Vector[Int]] = Map(M -> Vector(0), i -> Vector(1, 4, 7, 10), s -> Vector(2, 3, 5, 6), p -> Vector(8, 9))

如果不将 Map 值括起来,下面代码的 (k -> a :+ b) 键值部分将被视为 (k -> a) :+ b,因此会导致编译错误:

    (m, i) => m + (string(i) -> m.getOrElse(string(i), Vector()) :+ i)

// <console>:28: error: value :+ is not a member of (Char, Vector[Int])