any

Swift โŸฉ Features โŸฉ 5.6 โŸฉ any

้€™ๅ€‹ๆ–ฐ็š„้—œ้ตๅญ—ๆ˜ฏๅฐไฝฟ็”จ "existential types" ็š„ไธ€็จฎใ€Œๆจ™็คบใ€๏ผŒๆˆ–ๅฏ่ชชๆ˜ฏใ€Œ่ญฆ็คบใ€๏ผš

// โญ๏ธ it's OK for now (Swift: < 6.0 โœ…, >= 6.0 โŒ)
let a: SomeProtocol

// โญ๏ธ 'any' is required in Swift 6.0
let a: any SomeProtocol

the any keyword simply annotates that a given type is a so-called existential ... existentials are relatively expensive to use because the compiler and runtime canโ€™t pre-determine how much memory should be allocated for the concrete object that will fill in the existential ... the cost of using existential types should not be hidden, and programmers should explicitly opt into these semantics.

๐Ÿ‘‰ Donny โŸฉ What is the โ€œanyโ€ keyword in Swift?

Last updated