0

Why can't I write a function which returns an array of type Self, whereas I can do it for a single Self item?

I can write :

class func test(text: String) -> [Any] {
      return [
        Self.init(text: text),
        Self.init(text: text),
        Self.init(text: text)
      ]
}

But the compiler doesn't know the output types.

And [Self] does not work..

I need that for subclass reasons..

Thanks!

Thomas Albert
  • 275
  • 1
  • 10
  • See https://stackoverflow.com/questions/60643745/covariant-self-type-cannot-be-referenced-from-a-stored-property-initializer for a detailed answer around this problem. – flanker Mar 30 '23 at 18:11
  • Does this answer your question? [Covariant 'Self' type cannot be referenced from a stored property initializer](https://stackoverflow.com/questions/60643745/covariant-self-type-cannot-be-referenced-from-a-stored-property-initializer) – flanker Mar 30 '23 at 18:11
  • No because -> Self works actually, I have the issue only for returning an array of Self. I can use generic function, but I still need to infer the type using "as ..." when I'm using it.. – Thomas Albert Mar 31 '23 at 07:21

0 Answers0