Optional by Stuart Marks

Поделиться
HTML-код
  • Опубликовано: 12 сен 2024

Комментарии • 27

  • @MereAYT
    @MereAYT 4 года назад +7

    Very informative and well-presented. Thanks!

  • @wangjie8003
    @wangjie8003 4 года назад +3

    Best walk-through material that I could find in the internet!

  • @souvikadhikary8990
    @souvikadhikary8990 2 года назад +1

    So at the end , to summarise, If you are obsessed with functional programming & lambdas, go for optional, or else we should use the traditional way of coding style.

  • @bizoitz86
    @bizoitz86 7 лет назад +6

    Optional.get -> no arg Optional.orElseThrow

  • @DeepakPandey-ij3bz
    @DeepakPandey-ij3bz 5 лет назад +1

    As per rule 6 we should not use optional in collection. Then if fetch data from database and get value as null in a collection reference to the method. Then we should not use optional right. Then again we have to use null check. What is the point of creating optional then? Any suggestions on this.

    • @tanveerhasan2382
      @tanveerhasan2382 2 года назад

      Use optional as return value for methods, as mentioned in 35:45

  • @feddeschaeffer7116
    @feddeschaeffer7116 6 лет назад

    I feel like the object-oriented way to implement a ‘field that may not need to be stored in all objects that fulfill this interface’ would be defining a separate class of objects that don't store it, and implementing the interface with a method that returns whatever it needs to return there, such as Optional.empty().

    • @feddeschaeffer7116
      @feddeschaeffer7116 6 лет назад

      Would writing it that way feel out of place in Java?

    • @feddeschaeffer7116
      @feddeschaeffer7116 2 года назад

      For completeness, this question was a response to the scenario sketched at 30:45. I now understand that the speaker probably meant exactly the pattern I was thinking of with the term ‘null object pattern’: one class that has methods that do things using the value of the field and another class that implements those methods to do whatever is right when the field is not applicable.

  • @anuradharanasinghe9512
    @anuradharanasinghe9512 2 года назад

    Very informative. Thank you

  • @osematouati2430
    @osematouati2430 2 года назад

    I question for the geeks: in 28:00, I can't see the benefit of the second map (.map(Optional::of)) why is it included? in which use case is it significant.

    • @tanveerhasan2382
      @tanveerhasan2382 2 года назад

      Because we want the return value to be an optional as well.
      Without the 2nd map, orElse would return a BigDecimal since we are dealing with Optional.
      But the 2nd map turns that into Optional so when we call orElse we end up with Optional which is what we want

  • @grsalvi
    @grsalvi 6 лет назад +1

    So many places not use were mentioned but where to use optional then ?

    • @sobanya_228
      @sobanya_228 6 лет назад +2

      He says, you should never return null. Make return types typesafe with Optional

  • @jonathancamarena3117
    @jonathancamarena3117 2 года назад

    i guess this is why rust uses unrwap() instead of get()

  • @sobanya_228
    @sobanya_228 6 лет назад

    Where Optional violates monad laws?

  • @AlanDarkworld
    @AlanDarkworld 7 лет назад +4

    A lot of the pain of having to deal with Optionals would be eliminated entirely if only javac was capable of performing proper NULL analysis.
    Or take a page from functional languages:
    String? myvar; //

    • @spirridd
      @spirridd 7 лет назад

      You're talking about safe navigation operator.

    • @spirridd
      @spirridd 7 лет назад +1

      By the way you can use annotations like @Nullable and @NonNull.

  • @bernardobuffa2391
    @bernardobuffa2391 7 лет назад +9

    just horrible stuff.. will never use this monstruosity... Long live the null

    • @Omega3131
      @Omega3131 7 лет назад +5

      It's extremely useful in some cases.

    • @AndreaTortorella
      @AndreaTortorella 7 лет назад +5

      Most modern languages have this concept

    • @pockpicket9360
      @pockpicket9360 3 года назад

      it's probably one of the most usefull java feature

    • @bernardobuffa2391
      @bernardobuffa2391 3 года назад

      @@pockpicket9360 I think it just syntax sugar.. orElse... orElseThrow etc.. you can just replace all that with the old null testing... if(x==null) throw new MyException() if (x==null) x="default" etc. Am I missing something?

    • @Naglfar83
      @Naglfar83 3 года назад +1

      @@bernardobuffa2391 Yes, you are. Thing is, I don't want to check for null all the time. It's something that shouldn't be necessary. With Optionals I can respond to null values in a much more elegant way by just adding .orElse, .orElseGet or .orElseThrow. Checking for null is clunky, ugly and really old-fashioned.