scala / docs.scala-lang

The Scala Documentation website
http://docs.scala-lang.org
557 stars 1.01k forks source link

improve description about enumerator in for-comprehensions #2992

Closed laglangyue closed 4 months ago

laglangyue commented 4 months ago

Student of Scala may have some doubts when learning for-enumerators, and may not understand the execution order of if enumerator. They may think that if is similar to match grammer, they think if i + j == v will execute before j <-.... look at this example

    val a = Array(1, 2, 3)
    val value = for
      i <- 0 to 5;
      j = a(i) if i % 2 == 0
     yield (j)

Actually, it should be

    val a = Array(1, 2, 3)
    val value = for
      i <- 0 to 5 if i % 2 == 0
      j = a(i) 
     yield (j)
bishabosha commented 4 months ago

@scala/docs-zh

laglangyue commented 4 months ago

sorry,I check this PR again,My friend doesn't read the documentation very well. This section has explained very clearly how if works in for-comprehensions. image