X10

Does 'for' allow multiple comma-separated variables?

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: Language Specification
  • Labels:
    None
  • Number of attachments :
    0

Description

If you can have:

for (var i : Int = 1, j : Int = 11; ...)

Then the manual should say so.
(If not, maybe lang improvement?)

Activity

Hide
Igor Peshansky added a comment -

More importantly, can you have multiple for update statements, e.g.,

for (...; ...; i++, j--)

?

Show
Igor Peshansky added a comment - More importantly, can you have multiple for update statements, e.g.,
for (...; ...; i++, j--)
?
Hide
Bard Bloom added a comment -

Committed revision 13468.

Show
Bard Bloom added a comment - Committed revision 13468.
Hide
Igor Peshansky added a comment -

The following works:

public class XTENLANG_1173 {
  public static def main(Rail[String]) {
    for (var i:Int = 3, j:Int = 8; i < j; i++, j--)
      Console.OUT.println(i+"->"+j);
  }
}

and prints
3->8
4->7
5->6

Show
Igor Peshansky added a comment - The following works:
public class XTENLANG_1173 {
  public static def main(Rail[String]) {
    for (var i:Int = 3, j:Int = 8; i < j; i++, j--)
      Console.OUT.println(i+"->"+j);
  }
}
and prints 3->8 4->7 5->6
Hide
Bard Bloom added a comment -

But this doesn't:

def fearsome() = {
       

        for(var i : Int = 1, var j : Int = 2; i < 100; i++) {
    	   
       }
       
   }

So it looks like I can have only one statement, but a var statement can have several clauses.

Twelve of one, a half-dozen of the other, but probably worth noting in the manual there.

Thanks.

Show
Bard Bloom added a comment - But this doesn't:
def fearsome() = {
       

        for(var i : Int = 1, var j : Int = 2; i < 100; i++) {
    	   
       }
       
   }
So it looks like I can have only one statement, but a var statement can have several clauses. Twelve of one, a half-dozen of the other, but probably worth noting in the manual there. Thanks.
Hide
Igor Peshansky added a comment -

Even in C, for loops allow only one variable declaration statement as the initializer (but you can declare multiple variables in that statement).

Syntactically, X10 is more powerful than C here, because in C you can only declare variables of the same type, whereas the only thing they have to share in X10 is their mutability. However, the following fails:

for (var i: Int = 1, j: Double = 10.0; i < 10; i++, j--) {
   Console.OUT.println(i/j);
}

XTENLANG_1173.x10:3: Local variable declarations in a for loop initializer must all be the same type, in this case x10.lang.Int, not x10.lang.Double.
1 error.
Exception in thread "main" polyglot.util.InternalCompilerError: XTENLANG_1173.x10:3,26-41: Local variable declarations in a for loop initializer must all be the same type, in this case x10.lang.Int, not x10.lang.Double.
at polyglot.ast.For_c.typeCheck(For_c.java:133)
at polyglot.ast.JL_c.typeCheck(JL_c.java:180)
at polyglot.visit.TypeChecker.leaveCall(TypeChecker.java:106)
at x10.visit.X10TypeChecker.leaveCall(X10TypeChecker.java:101)

(no reason for it to fail except some misguided notion of C/Java compatibility).

Two variables of different mutability indeed cannot be syntactically declared in the for loop header in X10.

Show
Igor Peshansky added a comment - Even in C, for loops allow only one variable declaration statement as the initializer (but you can declare multiple variables in that statement). Syntactically, X10 is more powerful than C here, because in C you can only declare variables of the same type, whereas the only thing they have to share in X10 is their mutability. However, the following fails:
for (var i: Int = 1, j: Double = 10.0; i < 10; i++, j--) {
   Console.OUT.println(i/j);
}
XTENLANG_1173.x10:3: Local variable declarations in a for loop initializer must all be the same type, in this case x10.lang.Int, not x10.lang.Double. 1 error. Exception in thread "main" polyglot.util.InternalCompilerError: XTENLANG_1173.x10:3,26-41: Local variable declarations in a for loop initializer must all be the same type, in this case x10.lang.Int, not x10.lang.Double. at polyglot.ast.For_c.typeCheck(For_c.java:133) at polyglot.ast.JL_c.typeCheck(JL_c.java:180) at polyglot.visit.TypeChecker.leaveCall(TypeChecker.java:106) at x10.visit.X10TypeChecker.leaveCall(X10TypeChecker.java:101) (no reason for it to fail except some misguided notion of C/Java compatibility). Two variables of different mutability indeed cannot be syntactically declared in the for loop header in X10.
Hide
Bard Bloom added a comment -

Fixed, rev. 13537

Show
Bard Bloom added a comment - Fixed, rev. 13537

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: