11 ms·
conditional conformance is a game changer.
by adamnemecek 9y ago
conditional conformance is a game changer.
- euyyn 9y agoWhat is it?
- dbaupp 9y agoThe ability to state that a type only conforms to a protocol if its generic parameters satisfy certain bounds. https://swift.org/blog/conditional-conformance/ https://swift.org/blog/conditional-conformance/
- adamnemecek 9y agoIt let’s you say that e.g. two arrays are equatable if their elements are equatable.
- deleted 9y ago[deleted]
- O_H_E 9y agoDoes that exist in other languages, a quick look doesn't show up anything for python/c++
- pjmlp 9y agoIt might be possible in C++, with some template meta-programing love.
- masklinn 9y agoIt makes no sense in Python as it's a static typing feature. It makes limited sense for C++ as its generics are structural rather than nominative, though I believe Concepts will (eventually) introduce constrained templates: http://en.cppreference.com/w/cpp/language/constraints http://en.cppreference.com/w/cpp/language/constraints [0] It does already exists in other languages e.g. Haskell or Rust. For instance in Rust Vec's implementation of Eq is defined thus: impl Eq for Vec<T> where T: Eq {} So Eq is implemented on Vec<T> if and only if T implements Eq. Haskell is similar: instance (Eq a) => Eq [a] where (…) the bit before the "=>" is the constraint. [0] in current C++, you'd just define an `==` operator on your type, and it would blow up with obscure errors if you tried applying it incorrectly e.g. vector already overrides operator==, if you use it with a type which does not override == you get a few pages of templates garbage (287 lines in g++) boiling down to "==(vector<T>, vector<U>)" requires ==(T, U) but I couldn't find that definition. You can try it live with this (requires C++11): #include <iostream> #include <vector> struct S { uint8_t a; //bool operator==(const S& other) const { // return this->a == other.a; //} }; int main() { std::vector<struct S> v1 = { { 3 } }; std::vector<struct S> v2 = { { 4 } }; bool r = v1 == v2; std::cout << r << std::endl; } uncomment the operator== definition and it'll compile.
- dahauns 9y agoDunno about python (but then again it's a dynamic language...), and it's been an endless discussion for c++ AFAIK. Other than that it's actually rather common in statically typed languages (except Go ;P). From the top of my head e.g. Java, C#, Typescript, Kotlin, Rust, Haskell all offer constraints for generics.
- pjmlp 9y agoAdd CLU, Eiffel, BETA, Sather, ML to that list. :)
- masklinn 9y agoI think your comment is a bit… not sure what the word is, under-appreciating the feature? Over-selling some languages? Because AFAIK Java (or C#?) does not have constrained interface conformance, it only has bounds on functions (which Swift already had). That is you can say static <T extends Comparable<? super T>> void sort(MyList<T> list) such that you can only sort lists of comparables, but you can't say class MyList implements Comparable<T extends Comparable<? super T>> such that MyList implements comparable IIF its items are comparable.
- dahauns 9y agoAgh, yes. I already regret putting Java in that list with its wonky implementation. :) But it certainly works with others, e.g. something like this: public class SortedList<T> : ICollection<T>,IEnumerable<T> where T : IComparable<T>
- pornel 9y agoIt is in Rust: impl<T> Codable for Array<T> where T: Codable {} Also synthesized implementations are known in Rust as auto derive (#[derive(Eq, Hash)]). It's nice to see Swift and Rust agree on such features.