As the Swift Language Guide points out generic types can be constraint to classes or protocols like this:
public class MyGenericClass<T:Equatable> {
func printEquality(a:T, b:T) {
if a == b {
print("equal")
} else {
print("not equal")
}
}
}
Can I somehow restrict T
to be a struct
?
My use case is an observer class for value types that should only be used by structs.
As a side note: I know that there are e.g. class-only protocols, that are only implementable by classes. This is not really related, but shows that there is sometimes a special way to reach a goal.
protocol SomeClassOnlyProtocol: class, SomeInheritedProtocol {
// class-only protocol definition goes here
}