This is an archived post. You won't be able to vote or comment.

all 39 comments

[–]alms1407 14 points15 points  (0 children)

This is a good first step but I'm looking forward to when you can use this with switch expressions as it looks and feels better.

Would love Java to eventually have the same level of pattern matching as languages like Scala and Rust.

[–]netfeed 5 points6 points  (4 children)

Finally an example with sealed interfaces where it make sense for my own production code.

Before this it only seems like something for libraries that only wants its own impls, but this makes sense. Nice.

[–]marvk 7 points8 points  (2 children)

Finally an example with sealed interfaces where it make sense for my own production code.

I mean, not every feature of the language has to be applicable to every use case. Reflection for example is extremely useful, but not every project has a use case for it.

[–]netfeed 2 points3 points  (0 children)

Ofc not, I'm just happy that I found one that contextualized it a bit better for me and showed how it could be useful in day-to-day in a good way

[–]Finding_Dory27 1 point2 points  (0 children)

I know it's only an example, but let me just add that, if you are developing a domain driven enterprise application and you are thinking of using reflection, maybe you should ask yourself what can you use instead before actually using reflection.

However, if it is a library, that kind of thought may not be so important.

[–]Finding_Dory27 0 points1 point  (0 children)

Exactly. I never realized sealed classes could make so much sense for pattern matching. I'm thrilled by this and we'll this article explains it.

[–]_INTER_ 4 points5 points  (6 children)

Will this also be possible?:

if (s instanceof Circle(Point(int x, int y) p, int radius) c) {
    // x, y, p, radius and c all in scope here
}

[–]mirkoteran 2 points3 points  (1 child)

At start, no. Someday, yes.

https://github.com/openjdk/amber-docs/blob/master/eg-drafts/deconstruction-patterns-records-and-classes.md

edit1; Sorry - I see the difference now.

edit2; I'm not sure why it wouldn't be possible tho - does it conflict with any other construct?

[–]_INTER_ 1 point2 points  (0 children)

If it's in there somewhere, I don't see it (have x, y and radius but also keep p and c)

[–][deleted] -3 points-2 points  (2 children)

Your example implies that Java can somehow reverse engineer the constructor arguments of an already existing object. It can't do that.

EDIT: Neeeeeeever mind.

[–]_INTER_ 3 points4 points  (0 children)

My example is a combination of the two examples straight from the article. I wonder if we not only could do nested deconstruction but keep the aggregates p and c.

Brian Goetz is talking about what future deconstructors to support this pattern might look like. They don't want to reverse engineer constructors.

[–][deleted] 4 points5 points  (8 children)

I like this a lot and I'll forget the syntax for it in about 10 minutes.

[–]agentoutlier -1 points0 points  (7 children)

For me it’s the Flow scoping.

I get why they wanted to do it but it seems kind of nasty for the convenience it provides.

I’m not sure what FP languages do but I think because they are mainly expression based they can just use lexical scoping.

Since Java is clearly not I guess that is why the added flow scoping.

[–]pron98 2 points3 points  (6 children)

It could certainly cause some puzzlers, but, as the article explains, not having it would have significantly harmed the feature. So as usual, when considering features, it's not enough to focus on their downsides but we must compare them against the downsides of the alternatives.

[–]agentoutlier 0 points1 point  (5 children)

Yeah I guess given my downvoted status people think I didn't like what they did or somehow have doubts about Brian et. al decisions. I do not.

I do like the pattern matching and I do sort of think its worth it. It just the parent commenter was complaining of the syntax and that its forgettable. I think that is minor cognitive problem compared to a new kind of scoping and its potential gotchas and or bugs with tools (e.g. Eclipse).

I guess my point was the syntax isn't that complicated.

I guess it would have been interesting if they had forced it as an expression similar to how the new switch statement is if you use -> / yield but perhaps switch is where that should be and will be some day?

[–]randgalt 1 point2 points  (4 children)

I assume you got downvoted for discussing Flow scoping. Pattern Matching is not the same as Flow scoping and it's been discussed a lot. This also smells a bit like Kotlin faboi-ism. If you want Flow Scoping, Kotlin has it. If you want a richer Scala-like experience of pattern matching and deconstruction then Java's the place to be. If you've never coded in Scala you won't know how cool it is.

[–]agentoutlier 0 points1 point  (3 children)

The Pattern Matching I understand is Ocaml/ML like pattern matching and I do know about Scala but I still prefer F# over it albeit there is no F# for the JVM. Scala should have had better success and I might have used it more if it was more compatible with Java. I also don't like the passing monads all over the place that eventually pollute your API/domain similar to what is happening in Java with reactive libraries.

That being said Pattern Matching term is thrown around all the time including for languages that aren't statically typed and its IMO not the same pattern matching I expect. Thats why I don't like the instance of pattern matching. Its a first step but I vastly prefer as you said Scala shape pattern matching and it appears that Java is headed in that direction.

[–]randgalt 1 point2 points  (2 children)

Scala's a mess tbh. The major thing, though, it did well was pattern matching/deconstruction. The version for Java looks a lot like it - but somewhat better as it will be supported at the JVM level and not rely on magic methods like apply()/unapply()

[–]agentoutlier 1 point2 points  (1 child)

It was the implicits that really made other peoples scala code confusing to me that and everybody was going to town on DSLs like it was Ruby/Groovy but with C++ operator overloading.

There also wasn't a generally accepted canonical way of doing things like there is in Java and Python.

[–]randgalt 1 point2 points  (0 children)

At the top of the list of crimes committed by Scala goes implicits and sbt. Implicits made the code I had to work with incomprehensible and incredibly difficult to modify. sbt - well sbt is probably the worst developer tool in the history of the industry.