What's new in Swift 4.1
hackingwithswift.com
hackingwithswift.com
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?
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.
I imagine if you tried hard enough you could find edge cases that work on macOS but not on Linux, but the Linux support is complete enough for everyday production work.
Saying that they managed to keep concurrency orthogonal to the rest of the language means Rob Pike was "wrong" (or at least what i understood from his point).
However, after reading the conversations around the async proposal, it seems to me on the contrary that it is very tightly related to other parts (such as error throwing mechanism, GCD library and syntax in general).
We'll see. I wish i didn't have the feeling swift core team is understaffed, because at least for iOS development it is really a joy to program with.
Concurrency is still a work in progress in swift, in the most fundamental levels.
let mutex = DispatchQueue(label: “”)
mutex.sync { /* protected code here */ }
As for @sychronized, the devs have talked about why it is flawed (requires double locking) and why use should always use a manual mutex scope.EDIT: aren't you the one that wrote this article ? :))
There are probably goos reasons for doing it this way, but it always feels weird having to declare all your namespace's types inside one of its member.
I agree the language should have it, but it doesn’t seem like a particularly big deal.
...and part 2 here: https://medium.com/@qutheory/server-side-swift-vs-the-other-...
And here's summer 2017 benchmarking/comparing server side Swift frameworks vs. Node: https://medium.com/@rymcol/updated-benchmarks-for-the-top-se...
Swift is a great language.
Hello World in Java:
public class HelloWorld {
public static void main(String[] args) {
// Prints "Hello, World" to the terminal window.
System.out.println("Hello, World");
}
}Hello World in Swift:
print("Hello World")
Obviously Hello World is trivial, however that comparison is indicative of the elegance -- it has power similar to C with ease of use similar to scripting languages.
In the end, Swift is terrible. If you are developing on MacOS, stick with Objective-C. It’s a much better language.
If you're not a Mac or iOS developer, then it doesn't bring anything right now.
IMO, nobody is going to use this cross platform. Some people are trying to make it happen, but I doubt very much that it's going to be popular.
Well… in the sense that it is Yet Another Programming Language, you're right in some regard. Nonetheless…
> IMO, nobody is going to use this cross platform. Some people are trying to make it happen, but I doubt very much that it's going to be popular.
Those "some people" include IBM, who are behind the Kitura framework and are also porting Swift to their heavy PowerPC iron. They seem to be trying to make it into sort of a next-generation Java without the terribleness.
Yeah, it still maybe won't catch on as broadly as Java did, but it already has some enterprisey support outside of Apple. We'll see.
Never heard of them or used any of their software products. What other popular software have they brought to market?
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.)
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.
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/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/mast...
https://github.com/apple/swift-corelibs-foundation/blob/mast...
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.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.
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.But it certainly works with others, e.g. something like this:
public class SortedList<T> : ICollection<T>,IEnumerable<T> where T : IComparable<T>Except now that I look at it more specifically, it looks like a bound on the class's generic parameter rather than a condition on the interface implementation?
As in you're defining a SortedList class which implements ICollection and IEnumerable but requires T:IComparable, not defining a SortedList class which implements IEnumerable if T is IComparable.
And if that reading is correct, Java can do it just fine:
class SortedList<T extends Comparable<T>> implements Collection<T>, Iterable<T>
however it's got nothing to do with conditional conformance. 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.
[1]: gyb, a template processor, is a workaround for features that the standard library needs, but that are not (completely) implemented yet.
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).Just the words I want to hear when I upgrade my programming language by one minor version.
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
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.
Would you prefer hearing "We didn't fix anything in this update"?