Skip to content

Instantly share code, notes, and snippets.

@delexi
Created November 7, 2012 17:39
Show Gist options
  • Select an option

  • Save delexi/4033145 to your computer and use it in GitHub Desktop.

Select an option

Save delexi/4033145 to your computer and use it in GitHub Desktop.
for expression and apply syntactic sugar
val l = List("ab", "cd", "ef")
// Compiliert
(for(x<-l; if x.indexOf('c') >= 0) yield (l.indexOf(x), x.indexOf('c'))).apply(0)
// Compiliert nicht:
// <console>:9: error: type mismatch;
// found : Int(0)
// required: scala.collection.generic.CanBuildFrom[List[java.lang.String],(Int, Int),?]
// (for(x<-l; if x.indexOf('c') >= 0) yield (l.indexOf(x), x.indexOf('c')))(0)
(for(x<-l; if x.indexOf('c') >= 0) yield (l.indexOf(x), x.indexOf('c')))(0)
@kiritsuku
Copy link

Does not work because the for-expression is translated to a combination of higher order functions (map, flatMap, filterWith). All of these methods have a second parameter list expecting a CanBuildFrom and by writing xs.map(f)(0) this second parameter list is called instead of apply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment