Swift 3.1 Nested Generics Bug with Cyclic Metadata
Asked Answered
A

1

9

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!

Alcock answered 13/4, 2017 at 3:2 Comment(3)
It seems to work when I remove case others(T) for each Gadget and Company.Alcock
Are you "The Bob Lee"? :DEcphonesis
Haha, I go by Bob the Developer :)Alcock
B
9

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)
}
Bookstack answered 13/4, 2017 at 3:12 Comment(1)
Thank you :) I will study just a bit more about indirect enum!Alcock

© 2022 - 2024 — McMap. All rights reserved.