Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyword var in for loop not working #104

Closed
hamoid opened this issue Aug 28, 2019 · 5 comments
Closed

Keyword var in for loop not working #104

hamoid opened this issue Aug 28, 2019 · 5 comments
Assignees
Labels
enhancement New feature or request

Comments

@hamoid
Copy link

hamoid commented Aug 28, 2019

Hi :) Is this expected?

ArrayList<PVector> vv = new ArrayList<>();

void setup() {
  vv.add(PVector.random2D());
  vv.add(PVector.random2D());
  vv.add(PVector.random2D());
  
  // not good
  for(var v : vv) {
    println(v);
  }
  
  // good
  vv.forEach(v -> {
    println(v);
  });
}
@sampottinger
Copy link
Owner

sampottinger commented Aug 28, 2019

Hey there!

This fork moved us from a Java 5 to a Java 8 grammar which is why generics and lambdas work now. I think var was added in Java 10. So, it’s expected that the above snippet wont work but it doesn’t mean it’s desired.

Originally I was going to tackle moving to an 11 grammar in a separate PR as there’s some particularly meddlesome changes involved but ... we might still have some time on our hands ... so I’ll take a look again at moving the grammar to Java 11. Thanks!

@sampottinger sampottinger added the enhancement New feature or request label Aug 28, 2019
@sampottinger
Copy link
Owner

sampottinger commented Aug 28, 2019

Hey @hamoid! Thank you again for the bug report. The more I thought about it the more I realized that missing some of the new Java 11 features might be asking for some serious confusion (in particular, missing var is a bummer as more documentation online begins to adopt it). I have a working draft of this. I'll post shortly.

import java.util.*;
import java.util.function.*;

void setup() {
  List<String> list = new ArrayList<>();
  list.add("line1\nline2");
  list.add("line3");
  
  // Local variable type inference in loop
  for (var s : list) {
    println(s);
  }
  
  // Thanks https://codete.com/blog/java-8-java-11-quick-guide/
  IntFunction<Integer> testLambda = (var x) -> x * 2; // Type inference in lambda
  
  // Local variable type inference
  var testString = list.get(0);
  println(testString.lines().count()); // Java 11 API
}

@sampottinger
Copy link
Owner

#105 is passing CI! I'll do a little more testing this afternoon and merge in this evening. Chat soon!

@hamoid
Copy link
Author

hamoid commented Aug 28, 2019

Amazing! Thank you for doing this :)

@sampottinger sampottinger self-assigned this Aug 29, 2019
@sampottinger
Copy link
Owner

The merge is in!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants