Created
August 2, 2023 04:46
-
-
Save BrunoCerberus/2e2121cf9353a9189a0f9ba10838ae22 to your computer and use it in GitHub Desktop.
Opaque Return Types (some and any)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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