You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Dispatching a generic at run-time can cause problems when calling one generic to another. Specifically, the second generic function called from the first generic function loses its type information even though technically its available at compile time. This makes many techniques used in languages like C++ won't work in Swift.
For example:
protocolDispatchType{}classDispatchType1:DispatchType{}classDispatchType2:DispatchType{}func doBar<D:DispatchType>(value:D){print("general function called")}func doBar(value:DispatchType1){print("DispatchType1 called")}func doBar(value:DispatchType2){print("DispatchType2 called")}func test<D:DispatchType>(value:D){doBar(value: value)}letd1=DispatchType1()letd2=DispatchType2()test(value: d1)// "general function called", but expected "DispatchType1 called"test(value: d2)// "general function called", but expected "DispatchType2 called"
For the cases where this behavior is desired, a `@compile_time` attribute could be introduced to create the separate `test` functions so the desired behavior occurs. This would minimize the amount of code affected.
While using a protocol to get this behavior is possible, it can force large monolithic classes to occur (e.g. on stackoverflow). This modification would allow Swift to better support both an object orient and functional paradigm under these conditions.
The text was updated successfully, but these errors were encountered:
The problem is that protocols just don't cut it sometimes – a prime example of this is attempting to implement a method to work with all the elements of an arbitrarily deep nested collection (such as asked for in [SR-3638]). If there was a way to force Swift to specialise generic functions, and then dispatch based on that specialisation, you could say things like:
Additional Detail from JIRA
md5: 8203f9a4356a281611a01c8d139b40f2
Issue Description:
Dispatching a generic at run-time can cause problems when calling one generic to another. Specifically, the second generic function called from the first generic function loses its type information even though technically its available at compile time. This makes many techniques used in languages like C++ won't work in Swift.
For example:
For the cases where this behavior is desired, a `@compile_time` attribute could be introduced to create the separate `test` functions so the desired behavior occurs. This would minimize the amount of code affected.
While using a protocol to get this behavior is possible, it can force large monolithic classes to occur (e.g. on stackoverflow). This modification would allow Swift to better support both an object orient and functional paradigm under these conditions.
The text was updated successfully, but these errors were encountered: