<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi Robert,<br class=""><br class="">Sorry, I’m not sure I understand your question. In c++ you can do the following:<br class=""><br class=""><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class="">struct Storage {};</blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class="">struct CBlasStorage: Storage {};</blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><br class="">template <typename S> class Tensor {};</blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><br class="">template <typename S><br class="">Tensor<S> dot(const Tensor<S> &lhs, const Tensor<S> &rhs) {<br class=""> std::cout << "general version called" << std::endl;<br class=""> Tensor<S> result;<br class=""> return result;<br class="">}</blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><br class=""></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class="">// specialized version for CBlasStorage<br class="">template <><br class="">Tensor<CBlasStorage> dot(const Tensor<CBlasStorage> &lhs, const Tensor<CBlasStorage> &rhs) {<br class=""> std::cout << "specialized version called" << std::endl;<br class=""> Tensor<CBlasStorage> result;<br class=""> return result;<br class="">}</blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><br class="">// this preserves type information and will call the appropriate `dot`<br class="">template <typename T><br class="">void doSomething(const Tensor<T> &lhs, const Tensor<T> &rhs) {<br class=""> auto result = dot(lhs, rhs);<br class="">}</blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><br class="">int main(int argc, char **argv) {<br class=""> Tensor<CBlasStorage> a, b;<br class=""> doSomething(a, b); // we should get "specialized version called"<br class="">}</blockquote><div class=""><br class=""></div><div class=""><br class=""></div>The potential equivalent for Swift could look like:<div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">@_specialize_all</div><div class="">func dot<S:Storage>(_ lhs:Tensor<S>, _ rhs:Tensor<S>) -> Tensor<S> { … }</div></blockquote><div class=""><br class=""></div>Which would cause the compile to create a version of `dot` per S type that it gets called with. Thus, when `doSomething` is called, it would dispatch to that version of `dot`, allowing the type information to be preserved in the same way it does in c++.<div class=""><br class=""></div><div class="">Abe<br class=""><div class=""><br class=""><blockquote type="cite" class="">On Feb 5, 2017, at 11:35 AM, Robert Widmann <<a href="mailto:devteam.codafi@gmail.com" class="">devteam.codafi@gmail.com</a>> wrote:<br class=""><br class="">I don't understand how this change would cause method dispatch to invoke a different prototype. Specialization in either language mentioned doesn't do that.<br class=""><br class="">~Robert Widmann<br class=""><br class="">2017/02/05 11:28、Abe Schneider via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> のメッセージ:<br class=""><br class=""><blockquote type="cite" class="">Hi all,<br class=""><br class="">The current behavior of generics in Swift causes it lose type information at compile time due to the desire of maintaining a single version of the function. This runs counter to how c++ works, which creates a new copy of a function per type, but preserves information to be preserved. This can cause unexpected behavior from the user’s perspective:<br class=""><br class=""> protocol DispatchType {}<br class=""> class DispatchType1: DispatchType {}<br class=""><br class=""> func doBar<D:DispatchType>(value:D) { <br class=""> print(“General function called")<br class=""> }<br class=""><br class=""> func doBar(value:DispatchType1) {<br class=""> print("DispatchType1 called")<br class=""> }<br class=""><br class=""> func test<D:DispatchType>(value:D) {<br class=""> doBar(value: value)<br class=""> }<br class=""><br class=""> test(value: d1) // “General function called”, but it’s not obvious why<br class=""><br class=""><br class="">The suggested method to get around this issue is to use a protocol to create a witness table, allowing for runtime dispatch. However, this approach is not ideal in all cases because: (a) the overhead of runtime dispatch may not be desirable, especially because this is something that can be determined at compile time; and (b) there are some designs in which this behavior can complicate things.<br class=""><br class="">One example of a design where this behavior can be problematic is when a protocol is used to determine what functions get dispatched:<br class=""><br class=""> protocol Storage { … }<br class=""> class Tensor<S:Storage> { … }<br class=""><br class=""> class CBlasStorage: Storage { … }<br class=""> class OpenCLStorage: Storage { … }<br class=""><br class=""> func dot<S:Storage>(_ lhs:Tensor<S>, _ rhs:Tensor<S>) -> Tensor<S> { … }<br class=""><br class=""> // like behavior, these will not work if called from another generic function (but will work for non-generic functions)<br class=""> func dot<S:Storage>(_ lhs:Tensor<S>, _ rhs:Tensor<S>) -> Tensor<S> where S:CBlasStorage { … }<br class=""> func dot<S:Storage>(_ lhs:Tensor<S>, _ rhs:Tensor<S>) -> Tensor<S> where S:OpenCLStorage { … }<br class=""><br class="">In this case, depending on the underlying storage, we want an optimized version of `dot` to be called. To make this work correctly we can add static methods to `Tensor`, but this has several drawbacks: (a) it makes the `Tensor` class monolithic, every possible method must be determine a priori and be defined in the class; (b) it doesn’t allow new methods to be added Tensor without touching the main class; and (c) it unnecessarily forces users to user the more verbose `Tensor.dot(a, b)`.<br class=""><br class="">Point (a) in theory could be made better by creating a `TensorOps` protocols. However, because type constraints cannot currently be placed on extensions, it is not currently possible to implement.<br class=""><br class=""><br class="">One potential solution would be to add/extend an attribute for generic functions that would force multiple versions of that function to be created. There is already there is a `@_specialize` attribute, but you have to: (a) manually write out all the cases you want to cover; and (b) only affects the compiled code, which does not change this behavior. Due to the fact that `@_specialize` exists, I’m going to assume it wouldn’t be a major change to the language to extend the behavior to compile-time dispatch.<br class=""><br class=""><br class="">Thanks!<br class="">Abe<br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></blockquote></blockquote><br class=""></div></div></body></html>