To quote the official Swift.org blog:
Swift 5.8 also includes conditional attributes to reduce the maintenance cost of libraries that support multiple Swift tools versions.
#if
checks can now surround attributes on a declaration, and a newhasAttribute(AttributeName)
conditional directive can be used to check whether the compiler version has support for the attribute with the nameAttributeName
in the current language mode
As was mentioned in the original Swift Evolution proposal, it can be a bad idea to use #if compiler(>=5.6)
to check whether a feature is available. You might get the exact compiler version wrong, and the feature may not have been enabled yet.
This new method of specifying an attribute eliminates this problem, as the attribute will either be available or not.
Here’s an example of using it to check that @dynamicMemberLookup
is available.
When dynamicMemberLookup
is unavailable, this type and the view that uses it will not be available.