Search code examples
swiftgenericsnested-genericsswift3

Swift 3.1 Nested Generics Bug with Cyclic Metadata


First of all, thank you for visiting. I'm currently playing with Swift 3.1 Nested Generics and I've encountered an error with initialization.

class NestedProduct<T> {

  enum Gadget {
    case smartphone
    case laptop
    case fridge
    case others(T)
  }

  enum Company {
    case Samsung
    case Apple
    case Sony
    case others(T)
  }

  let company: Company
  let gadget: Gadget

  let reviews: [T]

  init(enterCompany: Company, enterGadget: Gadget, enterReView: [T]) {
    company = enterCompany
    gadget = enterGadget
    reviews = enterReView
  }
}

Now, I attempt to initialize

let product = NestedProduct<String>(enterCompany: NestedProduct.Company.Apple,
                                            enterGadget: NestedProduct.Gadget.laptop,
                                            enterReView: ["Good"])

However, I receive an error message,

GenericCache(0x11102a518): cyclic metadata dependency detected, aborting

I have no idea why this occurs. Could you guys please help? Thank you!


Solution

  • Looks like this is a known issue: https://bugs.swift.org/browse/SR-3779

    However, I was able to circumvent this by marking the enums as indirect. This will store associated values in another place which breaks the cyclic dependency.

    indirect enum Gadget {
        case smartphone
        case laptop
        case fridge
        case others(T)
    }
    
    indirect enum Company {
        case Samsung
        case Apple
        case Sony
        case others(T)
    }