Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #34747
| From | Daniel Pitts <usenet@coloraura.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Two surprises in for (:) |
| Date | 2016-07-01 15:08 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <nl6pks$68q$1@dont-email.me> (permalink) |
| References | <nl6apt$gg5$1@dont-email.me> <nl6f14$ukj$1@dont-email.me> |
On 7/1/16 12:07 PM, Eric Sosman wrote:
> On 7/1/2016 1:55 PM, Mike Amling wrote:
>> I was surprised that the javac compiler accepted the following:
>>
>> String[] foo={"a", "b"};
>> for (final String who : foo) {
>> }
>>
>> because it seems strange to have a for loop's variable work when final.
>
> Well, the "for loop's variable" is not a particularly rigorous
> description. Examples:
>
> for (int i=0,j=10; i < j; ++i,--j) ...
>
> for (; !theList.isEmpty(); theList.remove(0)) ...
>
> Neither of these has a unique obvious "loop variable."
>
>> After all,
>>
>> for (final int i=0; i<7; ++i) {
>> }
>>
>> gets an error.
>
> Yes, and I think that's unfortunate. Sometimes I want to use
> `i' in a lambda expression (or in a nested class), and I must write
>
> for (int i = 0; i < 7; ++i) {
> final int iFinal = i;
> // use iFinal in the lambda
> }
Or something along the lines: IntStream.range(0, 7).forEach(i ->
doSomethingWithLambda(() -> doSomethingWith(i));
> [snip]
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread
Two surprises in for (:) Mike Amling <mamling@chaff.us> - 2016-07-01 12:55 -0500
Re: Two surprises in for (:) Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-07-01 15:07 -0400
Re: Two surprises in for (:) Daniel Pitts <usenet@coloraura.com> - 2016-07-01 15:08 -0700
csiph-web