News for hackers. Stuff that matters.
Re:Java 20 Is Out! by akavi on Tuesday March 21, 2023 @11:39AM (#35247771)
Now even Java has pattern matching, what's Javascript's excuse?

Literally the only feature preventing Typescript from being my perfect language.

Re:Java 20 Is Out! by Waterluvian on Tuesday March 21, 2023 @11:41AM (#35247797)
https://github.com/tc39/proposal-pattern-matching

I'm not sure if it's stalled or not. But it's being discussed.

Re:Java 20 Is Out! by jackworks on Tuesday March 21, 2023 @12:05PM (#35248167)
We're still developing this proposal. be patient LOL
Re:Java 20 Is Out! by Waterluvian on Tuesday March 21, 2023 @12:13PM (#35248274)
I've got infinite patience for the brave souls who propose and implement TC-39 specs. =)
Re:Java 20 Is Out! by akavi on Tuesday March 21, 2023 @12:27PM (#35248514)
Sorry, that was rude of me. I know it's a ton of work, and am endlessly appreciative of the work you (I assume?) and others have been doing to push this forward.
Re:Java 20 Is Out! by javajosh on Tuesday March 21, 2023 @12:59PM (#35249037)
Wow that's a proposal! I really like the idea of pattern matching, and I'm glad you're steering clear of modifying switch, but I find the sketches in the proposal pretty hard to read, especially with the combinator examples. I hope you guys take your time and stay more proud of what you leave out than what you put in.
Re:Java 20 Is Out! by hn_throwaway_99 on Tuesday March 21, 2023 @01:41PM (#35249657)
The combinator example is the JS equivalent of what was discussed in another comment on this article, https://news.ycombinator.com/item?id=35247615. I like having the clarity of using different "and" and "or" keywords instead of overloading && and ||.

But I definitely agree with the sentiment - every new feature is another thing that a developer needs to learn if they encounter that code. There should be a large "barrier to entry" for new features.

Re:Java 20 Is Out! by TheRealPomax on Tuesday March 21, 2023 @01:29PM (#35249476)
This is something that far too few folks put in their progress/change logs.

"We didn't do ..." is so incredibly important.

Re:Java 20 Is Out! by BiteCode_dev on Tuesday March 21, 2023 @12:06PM (#35248178)
Given the terrible result we came up with in the Python community, I understand the JS wants to take their time. It's a great feature, but it's hard to get right, especially on an existing language.

Not to mention JS is not typed, but pattern matching that can't match typescript types would be terrible.

Re:Java 20 Is Out! by sgt on Tuesday March 21, 2023 @12:28PM (#35248528)
What's wrong with match? I've used it quite a bit and it is quite powerful.
Re:Java 20 Is Out! by vexna on Tuesday March 21, 2023 @12:40PM (#35248713)
Its incredibly powerful! One downside is variables captured in the match are lifted to the function level. The lack of block level scoping in the match statement leads to a lot of people shooting themselves in the foot by overwriting a variable that they didn't intend to. On a personal note, I find the 2 levels of indentation to reach the case body to be too much as well... but that's just a personal preference.
Re:Java 20 Is Out! by BiteCode_dev on Tuesday March 21, 2023 @12:38PM (#35248689)
The syntax is reusing regular python syntax with completely different effects, so you have to learn 2 syntaxes.

Trying to get advanced things such as "match a dict with variable key names and values, but you wish to enforce the number and the types and unpack them in variables" is ridiculously twisted to do, when it's even possible.

I use match/case, but it like the early years of type hints or asyncio: terrible ergonomics, and we know there is a ceiling to how much it can improve.

Re:Java 20 Is Out! by Yahivin on Tuesday March 21, 2023 @12:15PM (#35248310)
Re:Java 20 Is Out! by dgb23 on Tuesday March 21, 2023 @12:24PM (#35248444)
I don't see a huge benefit for pattern matching in JS outside of it being expression based rather than statement based.

Either way, the benefit has to be enormous. It's a feature that can easily break existing code.

Re:Java 20 Is Out! by int_19h on Tuesday March 21, 2023 @12:32PM (#35248600)
How would it break existing code?
Re:Java 20 Is Out! by dgb23 on Tuesday March 21, 2023 @01:32PM (#35249520)
Because it is an expression that introduces a new keyword.

You could say “return match…”

It’s not easy to introduce without breakage. Not saying it cannot be done.

But PHP didn’t manage to for example. It also isn’t real pattern matching but that’s a different issue.

Re:Java 20 Is Out! by int_19h on Tuesday March 21, 2023 @02:02PM (#35249953)
Oh, so you were referring to the specific syntax in the current proposal, rather than pattern matching in general?

Java and C# show how the syntax can be done in a fully backwards-compatible manner. It looks like it's just not the JS way by choice - apparently every new keyword that was added since ES5 is an actual keyword reserved in all contexts, not context-dependent?

Re:Java 20 Is Out! by dgb23 on Tuesday March 21, 2023 @03:33PM (#35251351)
That’s good to hear!
Re:Java 20 Is Out! by baq on Tuesday March 21, 2023 @12:45PM (#35248824)
In a world where upgrading node versions without language changes breaks existing code I wouldn’t be too worried.
Re:Java 20 Is Out! by Alifatisk on Tuesday March 21, 2023 @12:48PM (#35248877)
What’ the hype with pattern matching? What does it solve?
Re:Java 20 Is Out! by munificent on Tuesday March 21, 2023 @01:12PM (#35249227)
If you've ever done any assembly programming or worked with other old or low level languages, you may have encountered an environment where you can write simple operator expressions, but you can't compose them. So this is OK:

    a = b + c
    d = e - f
    g = a * d
But the compiler doesn't allow:

    g = (b + c) * (e - f)
You have expressions that produce, but they don't compose. You can't produce a value from a more complex, nested expression. We, rightly, no longer use languages like that.

Pattern matching parallels that, except for assignment and decomposing values. Many languages today let you write:

    topLeft = rect.topLeft.x;
    left = topLeft.x;
    top = topLeft.y;
    bottomRight = rect.bottomRight;
    right = bottomRight.x;
    bottom = bottomRight.y;
Or even:

    left = rect.topLeft.x;
    top = rect.topLeft.y;
    right = rect.bottomRight.x;
    bottom = rect.bottomRight.y;
(Because at least you can compose expressions on the RHS.) But they don't let you write:

    (topLeft, bottomRight) = rect;
    (left, top) = topLeft;
    (right, bottom) = bottomRight;
Or even:

    ((left, top), (right, bottom)) = rect;
Pattern matching gives you that. It is freely composable destructuring.

Also, the "matching" part means that in many languages you can also ask questions about values as you destructure them, which enables a particularly nice style of programming.

Re:Java 20 Is Out! by hn_throwaway_99 on Tuesday March 21, 2023 @01:27PM (#35249444)
All of your examples basically map to Javascript destructuring, which is already fully supported. Comment you are replying to is asking about pattern matching for flow control or conditional assignment, which JS doesn't currently support.
Re:Java 20 Is Out! by satvikpendem on Tuesday March 21, 2023 @01:31PM (#35249502)
Is Dart's pattern matching influencing the JS proposal as well? Or are both sort of separate without much influence from either side?
Re:Java 20 Is Out! by munificent on Tuesday March 21, 2023 @01:49PM (#35249772)
I have read the JS proposal many times, so it's an influence on Dart. I suspect Dart hasn't had much influence on JS because our proposal is newer.
Re:Java 20 Is Out! by vips7L on Tuesday March 21, 2023 @03:02PM (#35250897)
> the "matching" part means that in many languages you can also ask questions about values as you destructure them,

I started implementing Lox with Java's sealed classes + pattern matching on switch. The exhaustiveness has been really nice to ensure I cover each new token/expression as I add them.

Re:Java 20 Is Out! by brabel on Tuesday March 21, 2023 @01:11PM (#35249222)
Hype with pattern matching? That sounds quite funny to me considering certain languages (Haskell, Erlang, OCaml...) have had that for decades.

What convinced me of the power of pattern matching was seeing a red-black binary tree being implemented effortlessly in Ocaml (I think), while in C++ and Java it was a really difficult algorithm to implement.

When you have provably exhaustive pattern matching (i.e. the compiler forces you to handle every possible case), certain things that are very difficult to write otherwise become very easy.

Re:Java 20 Is Out! by nvarsj on Tuesday March 21, 2023 @01:34PM (#35249548)
It makes it very easy to deal with nested data structures. Imagine creating a nested map/struct literal, and then extract the values out using a very similar syntax on a single line.

It's one of those things that when you get used to, you wonder why other languages don't implement it.

Re:Java 20 Is Out! by Alifatisk on Tuesday March 21, 2023 @02:15PM (#35250177)
I think this answers the question very well, thank you. I found this [1] documentation from Ruby explain what it solves pretty good aswell.

[1] https://docs.ruby-lang.org/en/3.0/syntax/pattern_matching_rd...

Re:Java 20 Is Out! by dcminter on Tuesday March 21, 2023 @01:08PM (#35249171)
It's really nice syntactic sugar. Complicated conditional logic cluttered with redundant types turns into a series of simple patterns. "Just" makes it easier to not make stupid bugs (I'm all for it).
Re:Java 20 Is Out! by therealdrag0 on Tuesday March 21, 2023 @04:07PM (#35251788)
Agreed. Example from Scala

  // Given an Option
  val maybeThing: Option[String] = getThing()  

  // Classic
  if (maybeThing.isDefined) {
    useThing(maybeThing.get)
  } else {
    NotFoundResponseEtc()
  }  

  // Pattern matching (not too IDE auto generated exhaustive match cases!)
  maybeThing match {
    case Some(thing) => useThing(thing)
    case None        => NotFoundResponseEtc()
  }

This scales well when matching a higher cardinality of things like a variety of Exceptions or enums or other tuple responses like Either etc.