Java Withers - Inside Java Newscast #67

Поділитися
Вставка
  • Опубліковано 17 чер 2024
  • JEP 468 proposes a solution to the verbosity that can come from modeling mutable state with immutable records: derived record creation aka with expressions aka withers . That'll make it easy and succinct to create a copy of a record instance while changing just a few components.
    Links
    JEP 468: openjdk.org/jeps/468
    It's not names parameters: mail.openjdk.org/pipermail/am...
    Chapters
    0:00 Intro
    1:23 When Immutability Births Verbosity
    3:22 Derived Record Creation
    5:12 Nitty Gritty
    6:39 Named Parameters - Not!
    8:34 Brian Goetz on Named Parameters
    Tags #Java #JDK #OpenJDK
  • Наука та технологія

КОМЕНТАРІ • 51

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

    Nice enhancement. Re Brian Goetz's point about considering backwards compatibility of future enhancements and not just implementing a new cool feature: This is one of many reasons I was very comfortable coding in Java.

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

      Yes, exactly

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

    This new 'with' feature would have been nice to use when I was mis-using the Object#clone() method early in my career. Also, I saw the video title from my mobile. When I searched for it on the PC to actually watch, I almost got lost in a sea of Java 'Minecraft' Wither videos (not complaining).

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

    I fully understand the implications of implementing named parameters with default values, but please, continue exploring potential solutions. There must be a way to satisfy everyone's needs, even if it requires some compromise. This feature is undeniably valuable

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

    Loved the track on the background! Vvvzuuum!

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

    great

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

    Any thoughts about named parameters?
    Creating records with a lot of parameters is somewhat messy

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

    The actual problem for me is the public constructors in all records, some constrains cannot be maintain for several reasons and when that happens flexible contracts are a safe place: Builder pattern, defaults... I disagree with some lombok decisions and I usually omit it from my projects, but builder (with tobuilder) annotation saves plenty of work. Please, consider private constructors in records and some kind of builder pattern with defaults. That solution is portable to classes (it was there before) and already tested in other languajes. No need for `with` when you have an actual object and there is less chances to abuse that.

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

    Are there any plans to add the equivalent of @lombok.Builder so that we can finally can get rid of Lombok?

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

    Don't withers (or record deconstruction patterns for that matter) cause the exact problem regarding refactorability Brian brings up?
    Once you have clients using withers on records, refactoring a record type into a class would break that client code, or am I missing something?

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

      As it stands yes but I believe the plan is to bring withers to standard classes via the introduction of a “destructor” that would allow a class to be decomposed into its components

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

    In my mind, the more elegant solution would be to have built-in builders for records (like you would get from lomboks @Builder annotation) and give those builders an of() method. That way you could create a modified instance like so:
    var newPoint = Point.builder().of(oldPoint).x(42).build()

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

    At 5:27, the name of the record class is "Point5D", but the name "Point" is used to create its instance. Is that a mistake or I am getting something wrong?

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

    So, min 9:30, is 'with' also allowed for classes? otherwise, how does this do not break refactoring from records to classes?

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

      actually the problem of refactoring, especially for third-party libraries, can be attributed to a lot of things...

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

      That was exactly my thought when I listened To Biran Goetz part. If there is no wither for classes, then the path to refactoring from records to classes will be lost.
      One could presume they considered adding withers was more worthy than adding named parameters 😁

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

      @@ESGreenie To me, this just mean that we are going to soon get with for classes, where classes can implement some annotation to tell what is the 'canonical' constructor and what the argument names are. I do not see another way out of this funky situation. Or... drum roll..... deprecate the 'class' keyword!

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

    This seems great to me. Why not?

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

    so in records we can set the parameters value using its name? like ```end``` in python ``` print("hi", end=" ") ``` ?

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

    Javaland was great!

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

    Maybe we will get default value for method arguments one day. It pains me that I have to overload method when I want to have default values for methods

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

    Java WithUs :)

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

    And now the JEP has the Release field set to 23 already...

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

    lol Mike "String Template" Tyson

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

    'Whithers'?

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

      Ooops 🤦Fixed, thanks!

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

    I wish we had method parameter deconstruction. For example:
    boolean lineStartsAtXAxis(Line(Point(_, double y), _)) {
    if (y==0) return true;
    else return false;
    }

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

      Not with a method. but that should be possible with a switch or if instanceof with a deconstruction pattern , right?

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

    the wither method is look like the double braces instantiation if you look more into it
    public class Program {
    public static class Vector {
    {
    a = 10;
    b = 10;
    }
    public int a;
    public int b;
    }
    public static void main(String[] args) {
    var vector = new Vector() {
    {
    a = 0;
    b = 0;
    }
    };
    }
    }

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

    That looks daft!! convoluted and difficult. If only you had known Scala, you would have implemented a copy() method on the Record class. Just as Scala has been able to do with case class's (immutable values / types) for years! I once had a very brief email exchange with Brian Goetz, about having a copy method Record types. Immutability is great, but sometimes, it can be necessary to adjust an immutable value. Brian responded with "he saw no possible use case for such behaviour". 😀

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

    This is the same way how C# does, Java is more like copying from C#.

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

      The java from microsoft?

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

      Every language is copying from everyone else. As Brian Goetz have said many times, Java goes slowly forward in order to see which feature actually works well in other languages before including them. This way you won't end up in the mess which is C#, C++ and partly Kotlin.

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

      @@pompiuses Resume: java is a well structured language for business. Java still one of the more used languages on every site that you search, most people that don't like java is because they don't use java.

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

      @@pompiuses Calling C# or kotlin a 'mess' compared to java is... cute.

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

    Builders + withers , and we can convince teams to stop using Lombok.

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

    I certainly do not like the "record" construction.
    Go all-in for a regular Builder-pattern, not obfuscating it with most weird naming, have a closer look at Lombok and "@lombok.Builder(toBuilder=true)" because this is really, REALLY good!
    No new, half-backed semantics not looking like any living animal, duck or elephant, pleeease!

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

      Annotations are a bandaid not a cure.

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

      Annotations are not a good solution because they feel ad hoc.
      In the other hand Lombok it something Java developers had been force to live with to avoid half of the boilerplate code needed to manually implement builder pattern and getters and setters, but has been said several times that code generators are not advisable because they are actual code generated that programmers has not control of.

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

    Records pisses me off. Not because of records themselves, but because they make classes seem second-class (hah) citizens.
    Immutability has very little value for me in my day-job. Getting rid of boilerplate does.

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

      Immutable objects and values are very useful, especially if you want to access it from many threads at once (You can do that without creating race conditions - because it's immutable!).
      I'm using a mix of traditional OOP and Data Oriented Programming (not to be confused for Data Oriented Design)

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

    Golang fans tell that go's garbage collector is world class. No other language has such world's best and most performant gc.

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

    I don't like it. The simple Withers are good enough for 99% of all cases and for the rest you'd just create your own with-method that takes more than one parameter.
    And that works without any new syntax that people have to learn and that doesn't blend in well with the Java language and that works exclusively for records ("we want there to be a path to refactor records to classes") and without awkward limitations that are hard to understand for lots of people and catch you by surprise.
    I don't like the idea because it wants too much and is a typical case of over engineered YAGNI.
    Oh, and please, whoever reads this, before starting the argument about unnecessary object creation, read about escape analysis.

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

    One step closer to a `@lombok.Builder(toBuilder=true)` 😎

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

      The more steps from Lombok, the better.

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

      @@askarkalykov I guess you meant " one more step away", right? 😗

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

      @@delabassee I guess @mfe_ forgot to add word `functionality` at the end of their comment.

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

      @@askarkalykov i hear this all the time about lombok but when pressed 'why' it always boils down to some kind of irrational bias and 'what ifs' the NEVER happen IRL

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

    do you like Vada Pav?