Guards and Pattern Guards - TypeAlias Show Clip

Поділитися
Вставка
  • Опубліковано 9 лют 2025

КОМЕНТАРІ • 27

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

    Couldn't you add swift style guards to kotlin with something like `inline fun guard(check: Boolean, block: () -> Nothing) { if (!check) block() }`?

    • @Apollointhehouse
      @Apollointhehouse Місяць тому +3

      Then using said guard would be like `guard(order.placedByCustomerId == customer.id) { return }`, and you would be forced to short circuit since the lambda has a Nothing return type

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

      that looks very neat!
      and is totally in-line with how it would be implemented if it were an official part of the language; as many built-in features are just library functions, making use of the existing powerful language features

    • @typealias
      @typealias  Місяць тому +3

      Nice! 🎉 I'm going to play around with that and see what we can do with it! Thanks for sharing this.

    • @OsaSoft
      @OsaSoft 16 днів тому

      @@Apollointhehouse arrow has something similar with ensure(condition == true) { MyErrorType("foo") }, which shortcuircuits with the error in case the condition is NOT satisfied

  • @asadullahilyas
    @asadullahilyas Місяць тому +6

    How did you indent all arrows -> to right in when expression?

    • @typealias
      @typealias  Місяць тому +4

      Hey, Asadullah - thanks for asking! Just open up your settings/preferences and go to Editor > Code Style > Kotlin > Wrapping and Braces. Then scroll down that list and look for "Align 'when' branches in columns". Once that's set, then when you use the auto-formatter, they'll line up like that. Let me know if you have any trouble finding it!

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

      @@typealias Thank you, man. We all learn a lot from you. Keep up the good work.

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

    I like the idea of these guards. I've always been keen to short-circuiting to give a guaranteed state for the actual important logic or mutations and I can see the benefit of this experimental feature. What would be the timeline of it being brought to stable if it continues to move forward?

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

      I haven't yet seen an announcement about when they expect pattern guards to become stable... and there are some strong opinions out there about which keyword to use for it, or whether to include them at all. So for now, I suspect they're focused on just collecting community feedback about it.

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

    Hi,
    I saw that your website has all 20 chapters for free.
    Why buy the book in PDF?
    I would like to know if there are any other things in it besides the chapters that are advertised for free.
    Thanks

    • @typealias
      @typealias  Місяць тому +4

      Hey Yaniv, thanks for asking!
      - With the PDF edition, you can read the book offline and mark it up with highlights and notes, by using your PDF software of choice.
      - It also includes a table of contents and an index, each of which links to the topics.
      - I also just recently finished updating all of the drawings and annotated code snippets in the PDF so that they're now using vector illustrations, so you can zoom in without losing any quality.
      - Once I get done with a few more small clean-up tasks, I'm expecting to also add a bonus Question-and-Answer section to each chapter, which would be exclusive to the PDF and print editions.
      Previously, the PDF edition also gave early access to new chapters, but of course, now that all 20 are complete, there aren't any more chapters to get early access to. 🙂
      Hope that helps - let me know if you have any other questions!

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

      @@typealias Thanks for your answer.
      I would like to know: If I buy the PDF now. Will I receive updates of it when you finish with the bonus questions?

    • @typealias
      @typealias  Місяць тому +4

      Yes, whether you pick it up on Leanpub or on Fourthwall, you'll get access to the updates as I make them. With Leanpub, I only send email notifications about updates at particular milestones. With Fourthwall, you'll just get an email directly each time I push a new version.
      To get a feel for the frequency of updates I've made so far, you can see the release history on Leanpub here: leanpub.com/kotlin-illustrated - just scroll down to the "Release Notes" section.
      If you're on the fence about it, you could consider picking it up on Leanpub - they let you refund it within 60 days if it doesn't meet your expectations. So, if you're not seeing updates as quickly as you like, or if the upcoming Q&A sections aren't helpful for you, you could get a refund.
      Alternatively, you could just hold out another 4-6 weeks... I'm aiming to complete both the PDF and print editions within that timeframe. If you pick up the PDF on Leanpub at that point, you'd see the full set of bonus material and you could decide if you'd like to keep it.

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

    You have an opt-out option on your UA-cam channel that automatically translates the video and chapter titles to the main language of the viewer.
    Technical terms loose all their meanings once translated.

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

      Hey, thanks for mentioning that! Do you know the name of that option, or where I can find it? I just disabled the new Auto-Dub feature, but sounds like you might be talking about something else. Thanks!

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

      Hi, I have no idea of specific name of the option and from what I know it isn't retroactive so the changes will only be visible on new videos

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

      Okay, thanks. I'll poke around with this a little more and see if I can find it!

    • @ArthurKhazbs
      @ArthurKhazbs 23 дні тому

      This is why I always set English as my display language

    • @orghaniian
      @orghaniian 23 дні тому

      @@ArthurKhazbs then I will have the same issue with videos in my language, that's not a solution

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

    It’s increasing feature set without adding value.
    With ampersands I would be in, but the „if“ just looks strange.
    I’m not gonna use it, because it looks wrong.

  • @Mohammadkashifkhan2093
    @Mohammadkashifkhan2093 Місяць тому +3

    var me = "First"

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

      me = "Second" //😅

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

      @@h4m74ro me = "Third"

    • @HoussamElbadissi
      @HoussamElbadissi Місяць тому +6

      ​@@h4m74ro hahaha best cautionary tale against using `var` by default 😆

  • @codingCouncil
    @codingCouncil 18 днів тому

    hey dave , couldn;t we have just handled the special request.amount condition inside the RefundRequest branch itself ?
    fun receive(request: Request) {
    when (request) {
    is OrderRequest -> Warehouse.fulfillOrder(request)
    is RefundRequest -> {
    if (request.amount < 10) {
    println("Automatically refunded")
    } else {
    Warehouse.fulfillRefund(request)
    }
    }
    is SupportRequest -> HelpDesk.handle(request)
    }
    }
    Are there are pros and cons here . I didn't fully grasp the need of the guard conditions

    • @typealias
      @typealias  17 днів тому

      Hey CodingCouncil! That would work, but the disadvantage is that the compiler won't tell you when you haven't been exhaustive (at least, when it's a `when` statement rather than a `when` expression). For example, you could accidentally omit the `else` branch, and the compiler wouldn't tell you.
      On the other hand if you were to omit the else branch with a pattern guard like this...
      when (request) {
      is OrderRequest -> Warehouse.fulfillOrder(request)
      is RefundRequest if(request.amount < 10) -> println("Automatically refunded")
      // missing `is RefundRequest -> ...` here
      is SupportRequest -> HelpDesk.handle(request)
      }
      ... then you'll get a compiler error because it knows that you haven't been exhaustive.
      So, it works to write the code either way, but it's a matter of how much support you get from the compiler.