I know that we can essentially specify that our generics be any reference type by using AnyObject
:
class Foo<T: AnyObject> {
// ...
}
But is there a way to specify that our generics should only be value types, and not allow reference types?
A way to use Assertion:
Unit Tests:
Solution 1 via
extension
But as mentioned in the comments, it is working but not feasible, because user defined value types have to implement this protocol.
Solution 2 via method signature
Solution 3 via
assert
Another solution could be via
assert
"Solution" 4 via
where
clausesThis would be the best solution, I think. Unfortunately, it is not possible to have
or similar. Maybe it will be possible in future releases of Swift.