1

Say, I have a structure with a generic type parameter that requires a class type:

struct Foo<T: AnyObject> {}

As expected, I cannot use this structure with value types, but it works with class types:

// Error: 'Foo' requires that 'String' be a class type
let _  = Foo<String>()

// Working.
let _ = Foo<NSString>()

If I define a class-only protocol:

protocol Bar: AnyObject {}

I cannot, however, use the structure with this protocol:

// Error: 'Foo' requires that 'any Bar' be a class type
let _ = Foo<Bar>()

Why is that? And how would I work around this?

goofy4224
  • 141
  • 1
  • 8
  • A generic requires a concrete type. How you work around depends on your *specific* use case. – Sulthan Jan 30 '23 at 13:52
  • @Sulthan That was what I thought to but if you remove the `AnyObject` requirement from the `Foo` declaration, `struct Foo {}`, then the last line compiles. – Joakim Danielson Jan 30 '23 at 13:56

0 Answers0