Uploaded image for project: 'Swift'
  1. Swift
  2. SR-7622

Non-public typealias should be exported as public equivalent when possible




      In some scenarios it is useful to declare typealiases internally to a module, to make it easier to implement the functionality of the module itself, but less useful to impose that typealias on clients of the module. For example, consider an image manipulation framework that can work with NSImage or UIImage instances, depending on the platform. Internally to the module, I might define:

      #if os(macOS)
      	typealias RSPlatformNativeImage = NSImage
      	typealias RSPlatformNativeImage = UIImage

      Then I can implement methods, including public facing methods like:

      public func monochromeImage(fromImage image: RSPlatformNativeImage) -> RSPlatformNativeImage { ... }

      Since I haven't marked my typealiases as 'public', the above would fail to compile. But if I mark the typealiases public, I impose a new type "RSPlatformNativeImage" on clients, when as far as they are concerned NSImage or UIImage would do the trick.

      There's probably some generics + protocol way to tackle this, but that would also entail exposing unccessary type clutter (IMHO) to clients.

      What would be nice in this situation is instead of giving a compiler error, Swift could simply export the method using the public type that the typealias resolves to. In which case a client of the module on iOS would see the method as:

      public func monochromeImage(fromImage image: UIImage) -> UIImage { ... }

      Making Swift handle internal typealiases in this way would cause its behavior to match what is already done for Objective-C clients. The generated -Swift.h for this method in a Mac project is:

      - (NSImage * _Nonnull) monochromeImageFromImage:(NSImage * _Nonnull)image SWIFT_WARN_UNUSED_RESULT;

      Thus for Objective-C clients the clutter of the typelias definition is tidied away, but for Swift clients, it must be dealt with.


          Issue Links



              jalkut@red-sweater.com Daniel Jalkut
              0 Vote for this issue
              4 Start watching this issue