Java 23: Restoring the Balance with Primitive Patterns - Inside Java Newscast #66

Поділитися
Вставка
  • Опубліковано 5 чер 2024
  • The ongoing introduction of pattern matching to Java has put more weight on some language features than on others and unbalanced the language. In Java 23, primitive patterns will fix this regarding primitive types in `instanceof` and `switch`. This episode also goes over other upcoming patterns (deconstruction, static, instance, and constant) and shows how they will build out pattern matching in Java.
    JEP 455: openjdk.org/jeps/455
    Project Amber documents: openjdk.org/projects/amber/#d...
    Download JDK 23 EA: jdk.java.net/23/
    ~~~ Chapters ~~~
    0:00 Intro
    1:02 Existing Patterns
    1:23 Type Patterns
    1:32 Guarded Patterns
    1:53 Record Patterns
    2:09 Unnamed Patterns
    2:29 Nested Patterns
    2:50 Summary of Existing Patterns
    3:38 Primitive Patterns in `instanceof`
    5:17 Primitive Patterns in `switch`
    6:30 Primitive Patterns when Nested
    7:33 Upcoming Patterns
    7:59 Deconstruction Patterns
    8:40 Static Patterns
    9:09 Instance Patterns
    9:54 Constant Patterns
    10:37 Try JDK 23 EA!
    Tags: #Java #Java23 #OpenJDK #PatternMatching
  • Наука та технологія

КОМЕНТАРІ • 98

  • @Gennys
    @Gennys 2 місяці тому +34

    BigDecimal.
    I love that you used that clip, that guy is so funny. His video about senior Rust developers is too good.

    • @nipafx
      @nipafx 2 місяці тому +5

      I work in a corporate.

    • @nipafx
      @nipafx 2 місяці тому +2

      It's also linked as a card. The (i) in the top-right corner should pop open when I use it the first time.

    • @KangoV
      @KangoV Місяць тому +1

      The JS one hit so close to the bone. I loved it. He could do a new one per month!!!

  • @stealthmusic1
    @stealthmusic1 2 місяці тому +13

    I love pattern matching so much. When I used Erlang for some time, there were two things I missed a lot in Java. 1. Processes (-> Virtual Threads) and 2. Pattern Matching (which is partly already there. Great Stuff!

  • @nO_d3N1AL
    @nO_d3N1AL Місяць тому +5

    Never thought I'd see "Programmers Are Also Human" on the official Java channel! You guys are the best at DevRel 😄

  • @hkupty
    @hkupty 2 місяці тому +8

    Nice intro. So much patterns I couldn't understand what was going on there, but I could see the pattern.

    • @nipafx
      @nipafx 2 місяці тому +2

      :D In case you want to better understand how patterns work, check out this video: ua-cam.com/video/QrwFrm1R8OY/v-deo.html

    • @hkupty
      @hkupty 2 місяці тому +3

      @@nipafx that was mostly a bad joke, but I liked the segue to the other video

  • @Adowrath
    @Adowrath 2 місяці тому +4

    Instance Patterns are wild! The Regex one is something that's immediately visible, but the one with mapsTo is crazy. Might even be more efficient than an Option-returning Getter!

    • @prdoyle
      @prdoyle 2 місяці тому

      Very cool

  • @danthe1st
    @danthe1st 2 місяці тому +14

    switching over float sounds dangerous because typically, one wouldn't want exact equality checking due to the precision issues.

    • @nipafx
      @nipafx 2 місяці тому +4

      I can't recall having ever been in a situation where I wanted to switch over a float in the first place, although maybe I just never considered it, given that it was not allowed. :D I can imagine it may occur occasionally in the context of matching something else. That said, I don't really see a reason to switch over many specific values. 0, 1, maybe some constants like π (`Math.PI` is a double, though)... the rest would probably be guarded to compare to ranges (and I think upcoming patters will make that more succinct). So I don't worry too much about people comparing a float to arbitrary specific values but you are right that, if they do, that can easily create bugs - maybe something linters could have an eye open for.

    • @donwinston
      @donwinston 2 місяці тому +1

      Why can't the pattern match be a range test?

    • @nipafx
      @nipafx 2 місяці тому +7

      @@donwinston You can do that in 23 with a guarded pattern, but it isn't exactly elegant:
      case float f when 100 < f && f < 200 -> ...
      One could imagine a language feature targeting this specifically:
      case float f in [100, 200] ->
      But I don't know of any plans to add that and I guess that it won't be added. Once we can declare instance patterns, we should be able to get close to that with custom patterns:
      case range(100, 200).has(float f) -> ...
      The advantage of that is that it keeps the language conceptually simpler and allows custom logic that doesn't stick out, e.g.:
      case isPrime(float f) -> ...
      case powersOf(2).has(float f) -> ...

    • @danthe1st
      @danthe1st 2 місяці тому +1

      @@nipafx Even constants like 0 or 1 can be an issue. For example, there's a differentiation between positive and negative 0 or one could have a value that's almost 0/1/pi but not exactly. However, in the spirit of your range check reply, I could imagine something like
      case float f when inRange(f, delta)

    • @nipafx
      @nipafx 2 місяці тому

      @@danthe1st This is all theoretical of course, but I think use cases that want to execute a special branch for 0 or 1 would probably only like to take it, when the number is actually 0 or 1, not when it's just close to it. (The positive/negative issue is real, though, as can be seen by me not including negative zero in my example.)

  • @manoelcamposdev
    @manoelcamposdev 2 місяці тому +3

    Great improvements 🎉

  • @razt3757
    @razt3757 2 місяці тому +8

    6:14 this is why I think switch expressions should've used a different keyword for "switch patterns", like use "match()" instead of "switch()", and we wouldn't have this issue, they would be 2 different features completely.
    Using "switch" for both statements and expressions was a mistake imo.
    Other than that these are great features, looking forward to use them!

  • @npexception
    @npexception 2 місяці тому

    I really like where pattern matching in Java is going :)

  • @Ambusher306
    @Ambusher306 2 місяці тому +4

    Nice features :)

  • @user-jw9iw2zy1k
    @user-jw9iw2zy1k 2 місяці тому

    Hi, I want to know all these pattern matching, when comparing to if-else statements, would the compiler provide some magic behind the scenes that makes them run faster? or it's just some syntactic suger, that underneath it's the same if we just use if-else to replace them?

  • @nipafx
    @nipafx 2 місяці тому +5

    6:50 - when nesting switches goes too far 🤦🏾‍♂

  • @julianjupiter5890
    @julianjupiter5890 2 місяці тому +3

    `instanceof` keyword is so long. Why not introduce a shorter one? Perhaps `is`:
    if (o is String s) {

    }

    • @prdoyle
      @prdoyle 2 місяці тому +1

      The usual reason: adding keywords risks incompatibility with existing code. In this case, it would probably work though, because that "is" syntax doesn't have any existing meaning.

    • @Muescha
      @Muescha 2 місяці тому

      @@prdoyle one drawback is, when existing code also use a new keyword already - for example as a variable `is`. This would in some cases also break exisiting code.

    • @julianjupiter5890
      @julianjupiter5890 2 місяці тому +3

      @@Muescha probably, `InpuStream is = …;`.

  • @ehsanhasin8092
    @ehsanhasin8092 2 місяці тому +6

    Java 23 😘😘👌👌

  • @feoktant
    @feoktant 2 місяці тому +6

    So, pattern matching in Java becomes more and more similar to Scala, interesting

    • @avalagum7957
      @avalagum7957 2 місяці тому +2

      I wonder if Java will have head::tail and @tailrec some day

    • @feoktant
      @feoktant 2 місяці тому

      @@avalagum7957 at least collections without streams

    • @nipafx
      @nipafx 2 місяці тому +2

      @@avalagum7957 This was actually on the table for Project Loom when it was in its early stages. I can't recall the connection and I think it got dropped pretty early, but it's not like OpenJDK doesn't have it on its radar.

    • @Andr805
      @Andr805 2 місяці тому

      Because... Why not?

    • @feoktant
      @feoktant 2 місяці тому

      @@Andr805 I personally love it, as Scala-dev) but my Java-friends were always against it. Especially with underscore

  • @zombi1034
    @zombi1034 2 місяці тому +2

    Hey, what would be the best way to switch over an enum and ensuring exhaustiveness. I tried the following approach and while it works, it looks a little bit hacky. I also thought about turning it into a switch expression and returning a value that gets saved to an unnamed variable but this doesn’t work when the individual switch paths don’t return a value.
    enum Command {
    A,
    B,
    C;
    }
    var command = Command.A;
    // this compiles and checks for exhaustiveness
    switch (command) {
    case A -> System.out.println("A");
    case B -> System.out.println("B");
    case C -> System.out.println("C");
    case Command c when c == null -> {/* enables exhaustiveness */}​
    }

    • @Muescha
      @Muescha 2 місяці тому +2

      change last line to `case null ->` ?

    • @Muescha
      @Muescha 2 місяці тому

      JEP441

    • @zombi1034
      @zombi1034 2 місяці тому +1

      @@Muescha case null -> {} worked, thanks :)

  • @szaboaz
    @szaboaz 2 місяці тому +5

    Call me simple, but I sincerely hope that I won't have to maintain code anytime soon that are using these fancy pants features that were introduced since Java 8. It might be great for job security to use them though, as it will take people with several PhD-s to understand them.

    • @prdoyle
      @prdoyle 2 місяці тому +1

      You are simple.

    • @szaboaz
      @szaboaz 2 місяці тому +1

      @@prdoyle Haha, I know. It's a struggle. :) I'm looking forward to great study materials when things settle down a bit, but which author dares to write a book which is obsolete in 6 months? I don't know, how universities cope with the situation. One of Java's main strengths used to be that it's being thaught in unis as one of the main languages. Heck, one has to be on the edge if wants to get a recent Oracle Certification. Which will become obsolete in a minute. Crazy, no?

  • @thelaurg
    @thelaurg 2 місяці тому +4

    🎉 finally Java is getting closer to scala!

    • @KangoV
      @KangoV Місяць тому +1

      Oh no 🤣

  • @donwinston
    @donwinston 2 місяці тому +3

    Who decided the "switch" keyword is more appropriate than "match"?

    • @nipafx
      @nipafx 2 місяці тому +2

      Not sure. Maybe Dennis Ritchie when he designed C in the early 70s? Although, chances are he took his inspiration from another language, just like Java did. Or Swift, to pick a younger example.

    • @ITksh-zp1ob
      @ITksh-zp1ob 2 місяці тому

      backport compatibility

  • @terribleprogrammer
    @terribleprogrammer 2 місяці тому +2

    Does this help project Valhalla?

    • @nipafx
      @nipafx 2 місяці тому +6

      It doesn't help to finish Valhalla earlier but it starts fixing a problem that Valhalla will cause: When we can add our own primitive-like numerical types, we will very soon want to convert between them and between them and the onboard primitives but that can't be part of the language (because it doesn't know our custom types). This opens the door to code that asks "is this custom positive int an instance of Java's int?"

  • @reaper84
    @reaper84 2 місяці тому +1

    Oh my God... "Instanceof Byte" is then different from "instanceof byte"???? What happens if an "Integer" object is tested against that? If have the feeling this feature will create problems...

    • @charonstyxferryman
      @charonstyxferryman Місяць тому

      Quote,
      "Instanceof Byte" is then different from "instanceof byte"????
      Yeah, because Byte is an Object and byte is a primitive. A primitive has a value, i.e. not a reference to an object.

  • @zoladkow
    @zoladkow 2 місяці тому +1

    Hi there 😁 totally off topic, but it might be good to think of an extra pop filter in front of your mike man. I just happened to listen in headphones and maaan, thats some ASMR Java experience, i tell you 😁😁😁

    • @nipafx
      @nipafx 2 місяці тому +3

      I notice them during editing, too. But a pop filter is large, in the way of my gesticulating, and looks shitty. :( But I'll have to figure something out. Thanks for pointing this out - bursts my delusion that "probably nobody cares". :D

  • @VuLinhAssassin
    @VuLinhAssassin 2 місяці тому +1

    I had enough "patterns" to spend for the rest of my life, thank you 😂

    • @nipafx
      @nipafx 2 місяці тому

      Mission accomplished! 😅

  • @ramdaneoualitsen1323
    @ramdaneoualitsen1323 2 місяці тому +3

    too much is done for switch that we are rarely using

    • @nipafx
      @nipafx 2 місяці тому +4

      That's like saying "too much was done for lambdas, we are rarely using anonymous classes." 😉

  • @El-barto1
    @El-barto1 Місяць тому

    why not add support for default parameters

  • @stIncMale
    @stIncMale Місяць тому

    03:07 "Six fingers five bullets🤔"

  • @helderneres
    @helderneres 2 місяці тому

    It's Valhalla coming?

  • @hkupty
    @hkupty 2 місяці тому

    from now on I'll write all my java programs with gleam-style checks instead of ifs, so switch(boolean) { case true -> ....; case false -> ....;}

    • @Muescha
      @Muescha 2 місяці тому

      no more ternery operator?

    • @hkupty
      @hkupty 2 місяці тому

      @@Muescha that would be the dream

  • @kotlinsky.
    @kotlinsky. 2 місяці тому +1

    please make the deconstructor take only required fields according to name and type Point(int a), like in Rust. imagine if you have a record with 10 or more fields, it will be a horror _ _ _ _ _ _ _ _ _ _, int a, _ _

    • @IanFairmanUK
      @IanFairmanUK 2 місяці тому +2

      You could argue that any class with ten fields needs refactoring.

    • @kotlinsky.
      @kotlinsky. 2 місяці тому +1

      ​@@IanFairmanUK , right. but I still find it ugly to have to write useless _ _ _ _ _.
      on the one hand you make the code more readable/shortened, but on the other hand you force to write completely useless _ (kotlin-like moment with ternary operator)
      give me the ability to pull the fields I need out of the class/record, that's all

    • @nipafx
      @nipafx 2 місяці тому +1

      That's an interesting spin on the "named arguments" idea - I'll have to think about that. Named arguments themselves are not likely to appear any time soon, though: ua-cam.com/video/mE4iTvxLTC4/v-deo.html

    • @kotlinsky.
      @kotlinsky. 2 місяці тому +1

      @@nipafx If we are talking about refactoring problems in public libraries, if you add another field to record (at the end) that will not be processed in pattern matching (at least _), there will be problems as well, sooo...

    • @kotlinsky.
      @kotlinsky. 2 місяці тому

      @@nipafx I watched part of the video about mutable records. it's very unfortunate. immutability is useful in a multi-threaded environment, in the context of a game, where often everything is done by one thread, you force allocation of new objects for small changes. I also agree that you could make fields public final , the only advantage of methods is that if you change the name of the field, you will save the previously written code.

  • @Bas_Code
    @Bas_Code Місяць тому

    Why do i always missed your update?
    please don't want to be left behind

  • @Jankoekepannekoek
    @Jankoekepannekoek 2 місяці тому

    Hard disagree on the coersion for primitive patterns; type coersion is not the same as type casting (which is how reference patterns work).
    Furthermore you mention constant patterns are not useful on their own, but this is exactly how enum switches already work.

  • @OzoneGrif
    @OzoneGrif 2 місяці тому +1

    Not sure if I'm fond of these pattern matching, they are too dependant on the constructor declarations and types, and not enough on the field names.
    For using TypeScript everyday, I really love how you can deconstruct objects in TS; sometimes I dream of something similar in Java.
    The solution you show from Java 23 seems way too verbose and generates hard-coupling with your records structure. **Meh**

    • @nipafx
      @nipafx 2 місяці тому +3

      What you're describing seems to be named arguments, even if just for deconstruction. I get that that would be nice, but I'm not sure we'll get that. Here's Brian on named arguments in general: ua-cam.com/video/mE4iTvxLTC4/v-deo.html

  • @user-br4gt7xu2j
    @user-br4gt7xu2j 2 місяці тому +3

    does anybody use switch at all in projects instead of polymorphism or simple if-else? 😂

    • @yanchumak8419
      @yanchumak8419 2 місяці тому

      good question) typically something that based on strategy pattern)

    • @user-br4gt7xu2j
      @user-br4gt7xu2j 2 місяці тому

      @@yanchumak8419 simple map is much better than switch for strategy pattern, isn't it?

    • @nipafx
      @nipafx 2 місяці тому

      Sometimes polymorphism via inheritance is exactly what you don't want, e.g. because you'd rather not add all operations to the type. Check out this video for an explanation: ua-cam.com/video/QrwFrm1R8OY/v-deo.html

    • @OzoneGrif
      @OzoneGrif 2 місяці тому +1

      Almost never. Switches can most often be replaced by interfaces or polymorphism.
      I'll eventually use a switch or an instanceof pattern if I need to check if an instance implements an interface (to enable traits).
      But in general, I consider switches a code smell.

    • @avalagum7957
      @avalagum7957 2 місяці тому +1

      @@OzoneGrifCould you give an example of that code smell?

  • @amirkumalov4950
    @amirkumalov4950 2 місяці тому +1

    Please, leave "switch" alone!

    • @prdoyle
      @prdoyle 2 місяці тому

      No way. Switch has got so much better in the last few releases.

  • @sanaullahsianch9962
    @sanaullahsianch9962 2 місяці тому

    Learn AI and Python

  • @kotlinsky.
    @kotlinsky. 2 місяці тому

    about map.containsKey/points.get
    should we even think about operator overloading? and for maps map[key] = value , value = map[key]