Similar to a previous post, I noticed that normal and implicitly-unwrapped optionals are treated differently when it comes to protocol conformance; for instance:
protocol OptionalData {
var data: String? { get }
}
protocol UnwrappedData {
var data: String! { get }
}
// Compiles
struct SomeData1: OptionalData {
var data: String?
}
// Compiles
struct SomeData2: UnwrappedData {
var data: String!
}
// Does not compile: "type 'SomeData3' does not conform to protocol 'OptionalData'"
struct SomeData3: OptionalData {
var data: String!
}
// Does not compile: "type 'SomeData4' does not conform to protocol 'UnwrappedData'"
struct SomeData4: UnwrappedData {
var data: String?
}
Since the compiler clearly treats these differently when it comes to protocol conformance, is there any way of identifying the difference between a normal optional and an implicitly-wrapped one, either at compile time (say, with conditional conformance of a protocol) or at run time? I know the ImplicitlyUnwrappedOptional
type was removed, but I'm effectively looking for a way to replicate exactly that behavior.