4 ms·
JEP 443: Unnamed Patterns and Variables (Preview)
- cosmotic 4y agoMy $0.02: Unnamed variables just seem to add confusion and inconsistency. Inconsistency like this in Scala and Perl make it harder to learn and offer little value.
- speed_spread 4y agoThey allow to give out warnings on unused variables. If the warning is unwarranted and you really want to discard the value, you use _. It helps prevent mistakes by giving a hint on the intent of the programmer.
- cosmotic 4y agoI find it exceptionally rare to want not to use a variable that way, it's usually something you want to make harder to ignore, not easier. In the case you really do want to ignore, there are annotations to prevent the warning.
- bobbylarrybobby 4y agoThe underscore is the annotation
- cosmotic 4y agoI understand that; the problem is that it makes it so easy to bypass the safety net the warning is intended to provide. For example, not logging an exception; You'd have to be really intentional to block the warning with an actual annotation, but using an underscore would be so easy you'd just fall into a pattern of always commenting those out. It's better to have a line of code screaming "I'm doing something that looks wrong here but really it's okay" instead of "_".
- userbinator 4y agoThis code uses an enhanced for loop to step through a collection, calculating total as a side effect without using the loop variable order. That seems like a very contrived example. I don't work with Java regularly, but wouldn't that be better written as something like this? int total = max(orders.length, LIMIT);
- kaba0 4y agoIt is a contrived example, but the idea/code pattern isn’t — I have written plenty of code where I had to iterate over a collection with a for loop, but didn’t use the exact element. But obviously for typical usage one should probably just use the stream API.
- frant-hartm 4y agoOrders may be Iterable, which doesn't have length/size().
- yarg 4y agoNext to some of the more magic language features and major library changes, this really feels like beige syntactic sugar. Is it related to anything more useful?
- illiarian 4y ago> Is it related to anything more useful? To pattern matching.
- invalidname 4y agoI don't like it. Lots of the examples seem forced e.g.: var _ = q.remove(); This is just stupid. We can write: q.remove(); We don't need to save the return value in Java. The only case where this makes sense is: instanceof Point(_, int y) But that makes sense because the original code is so bad. A better solution would be type inference like we have in lambdas: instanceof Point(x, y)
- illiarian 4y ago> We can write: q.remove(); If that method returns, many tools will complain that the returned value is ignored
- invalidname 4y agoThat's a bug in the tools. IntelliJ doesn't and neither does Sonar Cloud.
- brabel 4y agoIntelliJ complains if you don't check certain values (e.g. `file.delete()`). It's not a bug, it's extremely helpful.
- invalidname 4y agoInteresting. It does do that for file.delete() but not for remove(). That's because it's smart enough to know which values are important. In the case of delete() you would want to check the result. So the warning is good and using an underscore to ignore it would actually be a problem. In the case of remove() we don't really need the result.
- kaba0 4y agoThere is type inference, like ` instanceof Point(var x, var y)`. The real winner here is obviously the used example, `instanceof ColoredPointer(_, var color)`, here every other solution would be plain ugly. The other examples are just there to make the feature intuitive - for example I quite like the `catch (SomeException _)`, making it exact that this exception is deliberately not used up in the catch block.
- kazinator 4y agoTXR Lisp: 1> (time-struct-utc (time)) #S(time year 2023 month 3 day 22 hour 4 min 25 sec 0 wday 3 yday 80 dst nil gmtoff 0 zone "GMT") 2> (match @(struct @type year @y) (time-struct-utc (time)) (list type y)) (#<struct-type time> 2023) We don't want to capture the type: use the @nil pattern which matches an object without capturing a variable: 3> (match @(struct @nil year @y) (time-struct-utc (time)) y) 2023 Wrong/nonexistent type: 4> (match @(struct foo year @y) (time-struct-utc (time)) y) ** expr-4:1: warning: if-match: no such struct type: foo ** match: @(struct foo year @y) failed to match object #S(time year 2023 month 3 day 22 hour 4 min 29 sec 32 wday 3 yday 80 dst nil gmtoff 0 zone "GMT") It's all macros, found in one file: https://www.kylheku.com/cgit/txr/tree/stdlib/match.tl https://www.kylheku.com/cgit/txr/tree/stdlib/match.tl If a student handed in a pattern matching system that didn't have a "null variable" pattern, I would not award full marks. That's just useless beyond words; you can't do things like (match (@a @nil @nil @c) ...) to match a four-element list, capturing the first and last element; you have to name the variables and then tell your compiler they are deliberately not used.
- kazinator 4y agoUnder the hood: 1> (compile-toplevel '(match @(struct @type year @y) (time-struct-utc (time)) (list type y))) #<sys:vm-desc: 986dff0> Disassembly of VM descriptor, annotated: 2> (disassemble *1) Constant data table (preinitialized registers D0 through D3): data: 0: year 1: t 2: match 3: @(struct @type year @y) Referenced functions, indexed by number: syms: 0: time-struct-utc 1: time 2: structp 3: struct-type 4: slotp 5: slot 6: list 7: sys:match-pat-error Actual code: code: 0: 20000003 gcall t3 1 1: 00000001 2: 2001000D gcall t13 0 t3 3: 00030000 Call (time), result in T3. Then (time-struct-utc T3) -> T13. 4: 20010002 gcall t2 2 t13 5: 000D0002 (structp T3) -> T2 6: 38000017 if t2 23 7: 00000002 If T2 is true, keep going, otherwise jump to 23 8: 20010009 gcall t9 3 t13 9: 000D0003 Call (struct-type T13) to get the structure's type object. 10: 2C0B0009 movsr t11 t9 This is a failure in dataflow analysis to remove a useless register copy. 11: 20020002 gcall t2 4 t9 d0 12: 00090004 13: 00000400 D0 is the year symbol; this is calling slotp to test whether the incoming struct object has a year slot. 14: 38000017 if t2 23 15: 00000002 If so, continue, otherwise go to error block at 23. 16: 20020008 gcall t8 5 t13 d0 17: 000D0005 18: 00000400 Now get the year slot's value. 19: 2002000C gcall t12 6 t11 t8 20: 000B0006 21: 00000008 List the type in T11 and the year value in T8, into a list held in T12. 22: 1000000C end t12 Execution ends returning T12. 23 is the error block: run-time function sys:match-pat-error is called with some arguments, like D3, which holds the source code of the pattern. 23: 20030002 gcall t2 7 d2 d3 t13 24: 04020007 25: 000D0403 Not reached, since that function throws: 26: 10000002 end t2 instruction count: 13 #<sys:vm-desc: 986dff0>
- signaru 4y agoLooks similar to Rust's underscore.