2

I'm trying to extend a struct that is already inside a struct. When I write the following code, I get

declaration is only valid at file scope.

struct A {
    struct AA {

    }
}

extension A {
    extension AA {

    }
}

Is it invalid to write an extension inside an extension?

haider_kazal
  • 3,448
  • 2
  • 20
  • 42
  • 2
    [The relevant portion of the language guide](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/doc/uid/TP40014097-CH34-ID378): *"Extension declarations can’t contain deinitializer or protocol declarations, stored properties, property observers, **or other extension declarations.***" – Hamish Jan 10 '17 at 10:39
  • @Hamish Correct, but not at all relevant. – Raphael Jan 10 '17 at 12:28
  • @Raphael Given the only explicit question OP asked was "*Is it invalid to write an extension inside an extension?*" – I'd say it's pretty relevant. – Hamish Jan 10 '17 at 12:29
  • @Hamish Right, the titular "question" and the one in the body have different answers. I missed that last line since it was pretty clear from the rest of the body what they wanted (which is not to discuss language specification but extend `AA`), my bad. – Raphael Jan 10 '17 at 12:31

1 Answers1

7

It seems like the only way to do that is:

extension A.AA
{
   func test()
   {
      print("Test")
   }
}

It just worked in my playground

Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161