Similar in spirit to this question, except, instead of limiting a protocol to only classes, I want to be able to define a protocol that can only be adopted by enums, structs etc.. Is this possible?
Asked
Active
Viewed 1,544 times
1 Answers
6
I couldn't find the answer, but through playing around, I've come to the conclusion that you can't. Being that to restrict a protocol to just classes, you precede it with class
like
protocol SomeProto: class {
func structYourStuff() -> Void
}
Making the assumption that this would be consistent among other types, I tried
protocol SomeProto: struct {
func structYourStuff() -> Void
}
But Xcode gave me five different errors on one line, which brings me to the conclusion that you can't. I could be completely wrong though, I only started learning Swift about a week ago

Chris
- 7,270
- 19
- 66
- 110
-
2Exactly, we cannot do this. IMHO, it would be a useful extension to the language. – CouchDeveloper Nov 10 '15 at 07:30
-
1@CouchDeveloper You got me thinking, perhaps there's some way to build out close functionality since we have the flexibility of custom operators. I can't think of how that would work though. – Chris Nov 10 '15 at 07:38