Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Java gets this right.

  for (int i : someCollection) {
      pool.execute(() -> something(i));
  }
Because "i" is effectively final, each Runnable closes over the correct value for that iteration. If "i" were being reassigned, the closure wouldn't be allowed to use it, you'd have to decide whether to close over a final copy or a reference to a mutable object that holds the latest value over time (a one-element array is the idiom).


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: