Opaque types: Masking your concrete types

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ก.ย. 2024

ความคิดเห็น • 20

  • @cil7ea
    @cil7ea 2 ปีที่แล้ว +1

    one of the best explanations out there

    • @swiftandtips
      @swiftandtips  2 ปีที่แล้ว

      Thank you very much! ❤️

  • @rungxanh2901
    @rungxanh2901 2 ปีที่แล้ว

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

    • @swiftandtips
      @swiftandtips  2 ปีที่แล้ว +1

      You’re welcome Joe! ☺️

  • @benhwang6378
    @benhwang6378 2 ปีที่แล้ว

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

  • @aleksandrzinovev429
    @aleksandrzinovev429 ปีที่แล้ว

    Thanks ) Really good explanation

  • @yali_shanda
    @yali_shanda 3 ปีที่แล้ว

    Very well presented, keep it up!

    • @swiftandtips
      @swiftandtips  3 ปีที่แล้ว

      Thanks for your support 😁🙌🏻

  • @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 🥲

  • @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!

  • @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 😂😂