struct MyStruct {
@ArrayOfNonOptionalElements var arr: [SomeNonOptionalType]
}
where @ArrayOfNonOptionalElements
is a propertyWrapper
.
So, how to force non-optionality for the type inside? Is it even possible? π
I mean, how to implement the property wrapper in such a way, that it forces the array element type to be non-optional.
Update:
Well, looks like nobody understood my question π£
I want the compiler to prevent putting a question mark inside the square brackets π
Update2:
Will copy/paste here one of my comments from under the post, so everyone could easily see it:
I'm writing "@Compact" propertyWrapper for safe array decoding. So the result will be guaranteed not to contain any nils. This property wrapper can operate with any array types. So it's rather just a kind of perfectionist challenge π
[SomeNonOptionalType]
it already guaranteed to not have nils. To have nils it would be[SomeNonOptionalType?]
. So what exactly are you trying to prevent.@ArrayOfNonOptionalElements
to a line of code just incase the same developer accidentally adds a?
to the rest of the array declaration? As I said, the compiler already helps you without needing any annotation.func i<T>(_ value: T) -> T { value }
, but limiting it soT
arbitrary can't beInt
. What harm would come if the constraint didn't exist? Generics are about constraining values by protocols, which in tern gives those generic values more power (because you have access to the protocol's requirements). If there's something that a non-optional can do that an optional can't, you should capture that in a protocol constraint.