I've just done some reading up on generics and protocols and I'm trying to implement an example. I'm clearly missing something.
For the code below, I've created a protocol called Themeable that I would like to use as a type to switch between themes. This will be done on a property called currentTheme on the struct SomeViewModel.
However, I'm running into an error:
Protocol 'Themeable' can only be used as a generic constraint because it has Self or associated type requirements.
Does that means it can only form part of a where clause and not be a type like I had hoped? If you could point me onto what I'm missing in the docs, that'd be ace!
You may be able to make SomeViewModel generic over a type that conforms to Themeable rather than requiring a protocol type here. But if not, allowing protocols like Themeable to be used as types was the subject of SE-0309. That change is not yet available in an official Xcode release, but if you're willing to wait, you can try a weekly snapshot toolchain.
I don't think it is. I was just following what I thought was convention.
Theme:Themeable is maybe more descriptive.
The whole idea was to switch out themes that have the same functionality that I could call. But if I have to define the type when I create SomeViewModel then I'm constraining myself to one type of theme.
Looks like it's back to the design drawing board for me. Using generics inside protocols is tricky.
Thanks for the information, and sorry for the rushed answer! The next Swift release will be great, with many life-enhancing features, and it's still difficult to keep in mind which ones will be available right away, and which ones will need runtime support