That's a pleasureTim Bant wrote:. . . thanks Campbell
Campbell Ritchie wrote:You don't seem to be using data anywhere, but it is a fresh member of the alphabet of variables at that point.
Education won't help those who are proudly and willfully ignorant. They'll literally rather die before changing.
Tim Bant wrote:One more ... why is it that Set doesn't produce any output?
when I pass print (List.of(1,2)) ?
Yes, that changed a long time ago, maybe even in Java9. I think it is called a pattern.Tim Holloway wrote:. . . . It was . . . syntactically invalid in Java 6, but . . . the Java 21 . . . instanceof changed to include a LocalVariableDefinition (Integer data) as an allowable RHS operand.
Why? Compilers don't usually make that sort of check.The compiler should probably have flagged "data" as an unused variable, though.
But your code should still compile first time.Tim Bant wrote:One more ... why is it that Set doesn't produce any output? . . .
Campbell Ritchie wrote:
Yes, that changed a long time ago, maybe even in Java9. I think it is called a pattern.Tim Holloway wrote:. . . . It was . . . syntactically invalid in Java 6, but . . . the Java 21 . . . instanceof changed to include a LocalVariableDefinition (Integer data) as an allowable RHS operand.
Campbell Ritchie wrote:
Why? Compilers don't usually make that sort of check.The compiler should probably have flagged "data" as an unused variable, though.
Education won't help those who are proudly and willfully ignorant. They'll literally rather die before changing.
If the type of the RelationalExpression is a subtype of the type of the Pattern, then a compile-time error occurs.
Tim Holloway wrote:
Campbell Ritchie wrote:
Yes, that changed a long time ago, maybe even in Java9. I think it is called a pattern.Tim Holloway wrote:. . . . It was . . . syntactically invalid in Java 6, but . . . the Java 21 . . . instanceof changed to include a LocalVariableDefinition (Integer data) as an allowable RHS operand.
No, "Pattern" is the other option.
Tim Holloway wrote:
Campbell Ritchie wrote:
Why? Compilers don't usually make that sort of check.The compiler should probably have flagged "data" as an unused variable, though.
Sure they do. All the time. I've even worked with C compilers whose lint-checking required a pragma to tell the compiler not to whine about it. It's a variant of the "statement has no effect" warning. In both cases, you'd want to know because at best it's lint, and at worst, you forgot something. Or haven't yet implemented something and it doesn't want you to forget.
Tim Bant wrote:I thought the pattern variable on the right cannot be the same as the left and must be a strict subtype, i.e. a subclass of Integer?
Mike Simmons wrote:So, for Tim Bant: the "strict subtype" rule you learned is indeed valid for Java 17, but not Java 21.
Mike Simmons wrote:
Allowing instanceof to have a local variable definition in the RHS is, indeed, called a pattern, and it happened in Java 16 (previewed in 14 and 15). JEP 394: Pattern Matching for instanceof
Education won't help those who are proudly and willfully ignorant. They'll literally rather die before changing.
Tim Holloway wrote:
Mike Simmons wrote:
Allowing instanceof to have a local variable definition in the RHS is, indeed, called a pattern, and it happened in Java 16 (previewed in 14 and 15). JEP 394: Pattern Matching for instanceof
Not in this case. Note the capitalizations I used for my terms. They come straight from the Java 21 JLS. This is a LocalVariableDefinition, not a Pattern. Those are the two options for RHS expressions on Java 21's instanceof operation.
Tim Holloway wrote:I wouldn't say the rules have been "relaxed", but rather altered to permit enhanced functionality.
Tim Holloway wrote:I actually don't know why one would choose to define a variable in an instanceof expression, but as of Java 12, it's syntactically valid. Presumably that meant it was important to someone.
Education won't help those who are proudly and willfully ignorant. They'll literally rather die before changing.
I can think of few places where instanceof actually constitutes good programming, but here is one such.Tim Holloway wrote:. . . I actually don't know why one would choose to define a variable in an instanceof expression . . .
To do a great right, do a little wrong - shakepeare. twisted little ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|