Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 778
Bob Lee Avatar asked Apr 13 '17 03:04

Bob Lee


1 Answers

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)
}
like image 125
Palle Avatar answered Oct 13 '22 14:10

Palle