How are any and some used in practice?
Asked on 2024-07-30
1 search
In practice, the use of any and some in Swift can be seen in the context of generics and type constraints. Here are some key points from the WWDC sessions:
-
Replacing
anywithsomein Embedded Swift:- In the session Go small with Embedded Swift, it is mentioned that using
anytypes can be avoided by replacing them with generics. For example, swappingany Countableforsome Countableturns a function into a generic function. This approach is beneficial as it allows the compiler to specialize generic functions, resulting in code that does not require expensive runtime support or type metadata.
- In the session Go small with Embedded Swift, it is mentioned that using
-
Disallowing
anytypes in Embedded Swift:- The session also highlights that
anytypes are disallowed in Embedded Swift to avoid the need for runtime metadata, which can be costly in terms of code size. Instead, generics are used to achieve similar functionality without the overhead.
- The session also highlights that
-
Using
anyin Swift Testing:- In the session Meet Swift Testing, the
expectmacro is discussed, which can accept any expression, including operators and method calls. This flexibility allows for detailed results if a test fails, showcasing the practical use ofanyin testing scenarios.
- In the session Meet Swift Testing, the
Relevant Sessions
These sessions provide insights into how any and some are used in practice, particularly in the context of Embedded Swift and Swift Testing.

Go further with Swift Testing
Learn how to write a sweet set of (test) suites using Swift Testing’s baked-in features. Discover how to take the building blocks further and use them to help expand tests to cover more scenarios, organize your tests across different suites, and optimize your tests to run in parallel.

Meet Swift Testing
Introducing Swift Testing: a new package for testing your code using Swift. Explore the building blocks of its powerful new API, discover how it can be applied in common testing workflows, and learn how it relates to XCTest and open source Swift.

Go small with Embedded Swift
Embedded Swift brings the safety and expressivity of Swift to constrained environments. Explore how Embedded Swift runs on a variety of microcontrollers through a demonstration using an off-the-shelf Matter device. Learn how the Embedded Swift subset packs the benefits of Swift into a tiny footprint with no runtime, and discover plenty of resources to start your own Embedded Swift adventure.
