Skip to content

simple inheritance  #111

Description

@robaho

Is there any way to do this?

@Codable
class A {
   var name : String
}
@Codable
class B : A {
   var location : String
}

@Codable
class Container {
    var elements[A] = [ B() ]
}

How do I annotate this so that when Container is decoded the correct instances of A/B are instantiated?

I got dynamic coding with a protocol working but I'd rather not use that. It seems it only works with protocols and enums?

I could code it by hand to work, but it seems the purpose of MetaCodable is to make something like this easy.

Activity

  1. soumyamahunt commented on Oct 25, 2024

    @soumyamahunt
    Contributor

    I got dynamic coding with a protocol working but I'd rather not use that. It seems it only works with protocols and enums?

    @robaho I am assuming you are talking about MetaProtocolCodable, you can use enums without having to use the MetaProtocolCodable. MetaCodable by itself provides untagged, external, internal and adjacent tagging enum decoding functionality. For your use case, it could look something like:

    @Codable
    struct Container
      let elements: [AOrB]
    
      @Codable
      @UnTagged
      enum AOrB {
        case b(name: String, location : String)
        case a(name: String)
      }
    }
    

    Please use discussion to ask these kind of queries. And use the proper templates when creating issues.

  2. added
    invalidThis doesn't seem right
    and removed
    bugSomething isn't working
    on Oct 25, 2024
  3. changed the title [-][Bug] simple inheritance [/-] [+]simple inheritance [/+] on Oct 25, 2024
  4. locked and limited conversation to collaborators on Oct 25, 2024
  5. converted this issue into a discussion #112 on Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    invalidThis doesn't seem right

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions