10 ms·
What's new in Swift 4.1
- adamnemecek 9y agoconditional 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.
- hacker_9 9y ago"things that didn't work before will now" Just the words I want to hear when I upgrade my programming language by one minor version.
- aplummer 9y agoThis is in relation to conditional conformance though, and very reasonable in that context.
- melling 9y agoYou are taking something out of context and commenting just to be snarky. This is not a case that you commonly use: var left: [String?] = ["Andrew", "Lizzie", "Sophie"] var right: [String?] = ["Charlotte", "Paul", "John"] left == right This works fine, for example: var left: [String] = ["Andrew", "Lizzie", "Sophie"] var right: [String] = ["Charlotte", "Paul", "John"] left == right
- Tehnix 9y ago> Just the words I want to hear when I upgrade my programming language by one minor version New features are not a breaking change? It will not affect any currently compiling programs. Also, as others have stated, that is quite a bit out of context.
- mcphage 9y ago> Just the words I want to hear when I upgrade my programming language by one minor version. Would you prefer hearing "We didn't fix anything in this update"?
- PopsiclePete 9y agoIs this used outside the Apple eco-system much? I admit I haven't looked at Swift in 2 years or more. I find the omission of features like namespaces really strange. I realize that namespaces don't jive with how Obj-C and Obj-C frameworks see the world and that Swift and Obj-C need to play together, but dealing with global namespace pollution and collisions in 2018 just seems...sad? They're hobbling the language in various ways due to NS*/Cocoa framework stuff. Another weird omission is lack of any kind of concurrency story. Right now, this just seems like Apple's "me-too" version of Java/C#, but what does it actually bring to the table that would make it a worth-while investment if I'm not an iOS developer?
- kainosnoema 9y agoMost Swift code is divided nicely up into modules, which are compiled together and have their own namespace. https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html#//apple_ref/doc/uid/TP40014097-CH41-ID4 https://developer.apple.com/library/content/documentation/Sw...
- thefounder 9y agoFor some reasons most of the swift code I've seen doesn't use any namespace/prefix when it calls various functions from different "modules". It's used just like C "include"
- plorkyeran 9y agoYou only have to qualify what module something is from if it would otherwise be ambiguous.
- eunoia 9y ago> Another weird omission is lack of any kind of concurrency story Grand Central Dispatch (GCD or libdispatch) is generally the concurrency story on Darwin platforms. There has been an ongoing effort to port it to Linux. Not sure how complete that is but I linked the Github below. Swift on Linux still has a lot of rough edges in general. https://developer.apple.com/documentation/dispatch https://developer.apple.com/documentation/dispatch https://github.com/apple/swift-corelibs-libdispatch https://github.com/apple/swift-corelibs-libdispatch
- beefsack 9y agoThis is great news for developers who use Apple products. I'm happy they're trying to improve the cross-platform story but I'm not sure I could ever completely trust the ecosystem when it's so tightly bound to MacOS/iOS (standard library still not complete, 3rd party libraries may only work or be well tested on Apple platforms, new features may take time to land on other platforms.)
- Cyberdog 9y ago> standard library still not complete Define "complete." I would say if you count the Foundation library - which ships with Swift - as part of the standard library, things are pretty darn complete. > new features may take time to land on other platforms Not as far as Swift itself is concerned. Ever since Swift went OSS, new releases land for Linux and macOS at the same time.
- euyyn 9y agoIs there already NSURLSession (or Swift's equivalent) for Linux?
- veidr 9y agoYes, and there has been for a while now. However, (as you might already know, since you chose the NSURLSession example), it's not 100% feature-complete when compared to the macOS or iOS version. Here's one example of where you have to do (or at least, I had to do on 2017-07-12) #if !os(Linux) to account for platform differences: https://github.com/soracom/soracom-sdk-swift/blob/master/Sources/Request.swift#L291 https://github.com/soracom/soracom-sdk-swift/blob/master/Sou... You will encounter that kind of thing from time to time if you use Swift and Foundation on Linux. Still, the Foundation library is highly usable on Linux, and is vastly ahead of where I expected they would be by now when they announced it. But there are still some things that are not implemented. They actually do have a good status page about this topic specifically: https://github.com/apple/swift-corelibs-foundation/blob/master/Docs/Status.md https://github.com/apple/swift-corelibs-foundation/blob/mast...
- 9y ago
- tabs_masterrace 9y agoCan anyone explain whats the reasoning behind renaming flatMap?
- masklinn 9y agoThe SEP makes it pretty clear: https://github.com/apple/swift-evolution/blob/master/proposals/0187-introduce-filtermap.md https://github.com/apple/swift-evolution/blob/master/proposa... Basically, they did not rename flatMap they renamed one specific overload: Sequence.flatMap<U>(_: (Element) -> U?) -> [U] or in Haskell parlance mapMaybe :: (a -> Maybe b) -> [a] -> [b] which filters after mapping more than it flattens (in fact the Rust and OCaml versions are called filter_map). The issue is that due to Optional promotion you could misuse flatMap (where you should have used map I guess) and could get this filtering behaviour unexpectedly instead of the straightforward mapping you would be expecting (it also led to extra branches and optional wrappings and unwrappings).
- dennisvennink 9y agoImplementing conditional conformances alone removed a lot of duplicate code from the standard library. I always like to think that the amount of gyb[^1] files in the source code is a good indicator of how far Swift is in its development. [1]: gyb, a template processor, is a workaround for features that the standard library needs, but that are not (completely) implemented yet.