3rd order expansion restriction with a common parameter

Here is what I want to achieve:

I have a general struct Future<Element> {} structure and another general Response<T> {} structure. I want to write a method that is in an extension for Future , which is valid only when the Element is Response<T> . It doesn't matter what T . So here is the code:

extension Future where Element == Response { }

But the fast compiler complains that the Link to the general "Answer" type requires arguments in <...> . Is there any other way to achieve this fast?

+5
source share
1 answer

One possible solution is to create a dummy protocol that Response will match:

 protocol ResponseObject {} struct Response<T> {} extension Response: ResponseObject {} 

Then you can check the protocol compliance in your extensions:

 extension Future where Element: ResponseObject { // ... } 
+2
source

Source: https://habr.com/ru/post/1269784/


All Articles