Opaque types: Masking your concrete types

Поділитися
Вставка
  • Опубліковано 8 вер 2024

КОМЕНТАРІ • 20

  • @rungxanh2901
    @rungxanh2901 2 роки тому

    Thank you Pitt for this. It's really visual and helps me understand the concept of opaque types much better 🙏

  • @cil7ea
    @cil7ea 2 роки тому +1

    one of the best explanations out there

  • @aleksandrzinovev429
    @aleksandrzinovev429 Рік тому

    Thanks ) Really good explanation

  • @benhwang6378
    @benhwang6378 2 роки тому

    It's really helpful ! Thanks for your video , Amazing !

  • @yali_shanda
    @yali_shanda 3 роки тому

    Very well presented, keep it up!

    • @swiftandtips
      @swiftandtips  3 роки тому

      Thanks for your support 😁🙌🏻

  • @adlsfreund
    @adlsfreund 3 роки тому

    Nice video! I didn't realize that opaque types is so important to SwiftUI, but it makes sense.
    I'm a bit confused about the very last part. Do you mean that protocols cannot *require* users to mark properties as for example `var body: some View` instead of allowing `var body: Text` ? Or were you referring to just the syntax in the protocol definition?
    Congratulations on the channel!

    • @swiftandtips
      @swiftandtips  3 роки тому +1

      Hi @adlsfreund, I meant the syntax. You cannot declare opaque types in protocols, which means you can return a concrete type for a property or method in a protocol’s implementation or use an opaque type because “some” is not part of the protocol, It’s optional for the user. And as I mentioned, the concrete type and the opaque type are “the same” internally for Swift 😉. Let me know if it makes more sense now.
      Also, check the swift proposal in the description.
      Thank you for your support 🙌🏻

    • @adlsfreund
      @adlsfreund 3 роки тому

      ​@@swiftandtips Thanks for your answer! I understand you now. Opaque types seems simple to me, but not sure about designing protocols with them in mind. I need more experience with protocols beyond just using SwiftUI.
      I'd like to see a video about type-erasing wrappers like AnyView. Why are they needed, and how to make my own? Thanks!

    • @swiftandtips
      @swiftandtips  3 роки тому +1

      Sure, actually Pretty soon I will talk about Combine and then introduce type erasing after seeing eraseToAnyPublisher(). Stay tuned 😉!

    • @adlsfreund
      @adlsfreund 3 роки тому

      @@swiftandtips Excellent!

  • @roman3249
    @roman3249 2 роки тому

    Hi. In the second example we actually have a sequence of integers. So can we somehow indicate that? Something like: "--> some Sequence" or "--> some Sequence where Element = Int"

    • @swiftandtips
      @swiftandtips  2 роки тому +1

      Hi Roman, yes, that’s possible on Swift 5.7+
      This video was recorded with Swift 5.4 🥲

  • @kyleclark1315
    @kyleclark1315 3 роки тому

    Awesome summation. Thank you for sharing your knowledge! regarding the use of Equatable at 20:09.. Is there a workaround or extension you can put on Animal, so that the protocol `Animal: Equatable` wont give an error for the signature `adoptPet() -> Animal`

    • @swiftandtips
      @swiftandtips  3 роки тому +1

      Hi Kyle, good question, unfortunately not. If you have to use Equatable for your protocol, then opaque types are the only way to do it, however you would lose flexibility of sending multiple types that conform same protocol. It’s a tradeoff, will depend on your needs.
      Thank you for the support!

  • @marduck19
    @marduck19 3 роки тому

    9:18 I am so lazy to read this 😂😂