Skip to content

Instantly share code, notes, and snippets.

@BrunoCerberus
Created August 2, 2023 04:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrunoCerberus/2e2121cf9353a9189a0f9ba10838ae22 to your computer and use it in GitHub Desktop.
Save BrunoCerberus/2e2121cf9353a9189a0f9ba10838ae22 to your computer and use it in GitHub Desktop.
Opaque Return Types (some and any)
import Foundation
// Basically opaque return types you delegate the
// inferring type to the compilator instead of defining
// explicitly its type.
// with some
func getData() -> some Collection {
return [1, 2, 3, 4, 5]
}
// with any
func getAnotherData() -> any Collection {
return Bool.random() ? [1, 2, 3, 4, 5] : ["one": 1, "two": 2]
}
// Notice that each variable type is only
// computed in compilation process, so before the compilation
// you don't know the variables specifically types.
let data = getData()
data.count
let anotherData = getAnotherData()
anotherData.count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment