Skip to content

Instantly share code, notes, and snippets.

@foxicode
Created February 15, 2023 21:02
Show Gist options
  • Select an option

  • Save foxicode/833e43c644f9a272d6e26c94b3c5891f to your computer and use it in GitHub Desktop.

Select an option

Save foxicode/833e43c644f9a272d6e26c94b3c5891f to your computer and use it in GitHub Desktop.
Existential Any
import Foundation
protocol User: Identifiable {
var firstName: String { get set }
var lastName: String { get set }
}
func printUserName(_ user: any User) {
print("\(user.firstName) \(user.lastName)")
}
enum AdminRole {
case manager
case finances
case support
}
struct Admin: User {
// Identifiable
var id: Int
// User
var firstName: String
var lastName: String
var roles: [AdminRole]
}
let admin = Admin(id: 1, firstName: "John", lastName: "Doe", roles: [.manager, .finances])
printUserName(admin)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment