Understanding Opaque Return Types in Swift

Hi, Habr. As part of the course “iOS Developer. Professional ” prepared a translation of useful material for you.



We also invite you to take part in
an open webinar on the topic "Writing an application in SwiftUI and Combine" . Participants, together with an expert, will understand what SwiftUI and the Combine framework are, as well as how to create a small application with their help.





You can also read this article on my Xcoding With Alfian blog by following the link .





Opaque return types ( ) — , Apple Swift 5.1. (some



) (function



)/ (method



) (property



), , API. , (protocol



). API- , opaque type



some. Swift (identity



) , . SwiftUI opaque return types



View, some



View body



.





, opaque return types



, , API Swift:





  1. , (concrete



    ) API (encapsulation



    ).





  2. API , , , .





  3. , . , .





  4. - . opaque protocol type



    , Self associated type



    .





  5. . opaque return types



    , .





Opaque return types

opaque return type , , .





associatedtype

, MobileOS. associatedtype



( ) Version , Version, , .





protocol MobileOS {
    associatedtype Version
    var version: Version { get }
    init(version: Version)
}
      
      



: iOS Android. . IOS float, Android String ( Android 10).





struct iOS: MobileOS {
    var version: Float
}
struct Android: MobileOS {
    var version: String
}
      
      



, , MobileOS . , :





1 ( ):

func buildPreferredOS() -> MobileOS {
    return iOS(version: 13.1)
}
//  
 'MobileOS'      , 
        Self  associated type.
      
      



, , , associatedtype



. . , .





2 ( ):

func buildPreferredOS() -> iOS {
    return iOS(version: 13.1)
}
//   
      
      



, , , API . , Android .





3 (Generic Function Return)

func buildPreferredOS<T: MobileOS>(version: T.Version) -> T {
    return T(version: version)
}
let android: Android =  buildPreferredOS(version: "Jelly Bean")
let ios: iOS = buildPreferredOS(version: 5.0)
      
      



, . , API, . , , .





( Opaque Return Type)

func buildPreferredOS() -> some MobileOS {
    return iOS(version: 13.1)
}
      
      



opaque return type



, , , MobileOS . , , MobileOS.





Opaque returns types

, , , opaque return type



, .





func buildPreferredOS() -> some MobileOS {
   let isEven = Int.random(in: 0...100) % 2 == 0
   return isEven ? iOS(version: 13.1) : Android(version: "Pie")
}
//  
     'iOS' 
    'some MobileOS'

func buildPreferredOS() -> some MobileOS {
   let isEven = Int.random(in: 0...100) % 2 == 0
   return isEven ? iOS(version: 13.1) : iOS(version: "13.0")
}
//    
      
      



, opaque return value



. .





opaque return type API

opaque return value



— , Opaque return type



, opaque protocol type



, .





, , generic constraint



, numeric



. :





  1. .





  2. , .





API , for



(print) .





.





1. Generic Return Function





func sliceFirstAndEndSquareProtocol<T: Numeric>(array: Array<T>) -> LazyMapSequence<ArraySlice<T>, T> {
   return array.dropFirst().dropLast().lazy.map { $0 * $0 }
}
sliceFirstAndEndSquareProtocol(array: [2,3,4,5]).forEach { print($0) }
// 9
// 16
      
      



, LazyMapSequence, T>



, .





2. Opaque Return Types





func sliceHeadTailSquareOpaque<T: Numeric>(array: Array<T>) -> some Sequence {
    return array.dropFirst().dropLast().lazy.map { $0 * $0 }
}
sliceHeadTailSquareOpaque(array: [3,6,9]).forEach { print($0) }
// 36
      
      



, , sequence, .





Opaque Return Types SwiftUI

SwiftUI , body View , View. .





struct Row: View {
    var body: some View {
        HStack {
           Text("Hello SwiftUI")
           Image(systemName: "star.fill")
        }
    }
}
      
      



body



:





HStack<TupleView<(Text, Image)>>
      
      



, , , view



HStack. Opaque return type



SwiftUI. API View, View.





Opaque return type

opaque return type



, , . , Apple.





apple/swift-evolution





, …





github.com





Opaque Types — Swift (Swift 5.1)





opaque return type . …





docs.swift.org





Swift - WWDC 2019 - - Apple Developer





Swift Apple, …





developer.apple.com





, Swift, Swift WWDC 2014. , , API, , associatedtype



, Codable



.





, Apple Swift , , Swift Evolution. , Swift Language ( struct Swift 5.1 ).





Swift . , - . , , , . !






«iOS Developer. Professional».



« SwiftUI Combine».








All Articles