why can't I call methods on a for-yield expression?

Posted by 1984isnotamanual on Stack Overflow See other posts from Stack Overflow or by 1984isnotamanual
Published on 2010-06-02T06:08:58Z Indexed on 2010/06/02 6:13 UTC
Read the original article Hit count: 149

Filed under:

Say I have some scala code like this:

// outputs 1, 4, 9, 16, 25, 36, 49, 64, 81, 100
println( squares )

def squares = {
    val s = for ( count <- 1 to 10 ) yield { count * count }
    s.mkString(", ");
}

Why do I have to use the temporary val s? I tried this:

def squares = for ( count <- 1 to 10 ) yield { count * count }.mkString(", ")

That fails to compile with this error message:

error: value mkString is not a member of Int
   def squares = for ( count <- 1 to 10 ) yield { count * count }.mkString(", ")

Shouldn't mkString be called on the collection returned by the for loop?

© Stack Overflow or respective owner

Related posts about scala