Swift – observations from Rust’s original designer
graydon2.dreamwidth.org
graydon2.dreamwidth.org
EDIT: Does anyone know if it's possible to try it without a paid iOS / OS X dev license? I'd especially like to try the playground, but I'm not willing to dump $100 on it.
It is pretty disappointing that Apple is walling in Swift like this. I was excited about it to begin with, because it looks like a great language, but apparently as Microsoft moves toward openness, Apple is doing the opposite. Now I'm not really sure what to do with it.
That means it is still under NDA as well. Most likely after Xcode 6 is released publicly (i.e. available on the Mac App Store) they will do a code dump back to the LLVM project.
Partially. They made the language manual freely available on the iBookstore.
This happens pretty often in their betas. First few iterations of even new frameworks can end up doing vastly different things than later versions.
And when iOS 8 and OS X Yosemite are released this fall, you can submit apps that use Swift to the App Store and Mac App Store.
This is intended as a beta, and they won't even allow you to use it in production yet, even if you wanted to, so you can expect some small changes to the language spec as they refine it. Typically the xcode betas can't be used to submit, and Apple have released other projects closed source before open sourcing them (for example webkit).
It's interesting that they're moving development on both platforms to this new language. I wonder when we'll see a unified API?
A proprietary Swift even sort of makes logical sense. I could easily see Apple thinking that since Swift is intimately tied to its proprietary system frameworks, Swift should be proprietary too.
Once Xcode/Yosemite are no longer beta they will be released for free, just like the existing Xcode that is freely available from the MAS.
Rust isn't stable. It really hasn't seen any serious use.
Even the Rust home page itself currently states, "Rust is a work-in-progress and may do anything it likes up to and including eating your laundry."
At worst, it says that Rust's toehold is tenuous enough that even vaporware could replace it. I don't agree with that, but I can understand the sentiment. Think "knocked back with a feather".
That said, it'd be nice to see Swift get open sourced at some point.
- Chris Lattner, http://nondot.org/sabre/
I find `if let concrete = optional` sooo much nicer than `match optional { (concrete) => , _ => {} }`.
Rust has solid semantics that covers more than Swift. OTOH Apple has put their UX magic into the language's syntax. Some syntax shortcuts, like `.ShortEnumWhenInContext` are delightful.
The two combined will be the perfect language ;)
if concrete = optional
call concreteThe special things about the way it works in Swift are:
- You have to put let in front, thus avoiding the confusion.
- If the RHS is optional it unwraps the option for the success branch, but the variable is not available in any other scope, thus ensuring safe use.
In JS, C, CS, Ruby, etc. you're not really doing anything useful if you assign a value to another name just for one branch of an if statement. In Swift you are.
macro_rules! if_let {
($p:pat = $init:expr in $e:expr) => {
{ match $init { $p => $e,_ => {}, } }
}
}
fn main() {
let tup = (2, 3);
if_let!{(2, x) = tup in println!("x={}", x)}; // prints x=3
if_let!{(5, x) = tup in println!("x={}", x)}; // doesn't print
}
It's slightly more heavyweight, but still not too bad.The Smalltalk form is actually more powerful, since it gives you a kind of multiple dispatch (a.la. C++ argument overloading) that would be quite expensive in a non-statically typed language otherwise.
Many language constructs were invented simultaneously under different names in different languages.
The language seems more closer to F# than C# to me. It's all good though!
Why couldn't he have just have written "(Swift ∖ ObjC) ∩ Rust"? :)
The biggest thing I see is that it's an imperative language with functional-style ADT's, tuples, sum types, etc.
It also looks fairly similar syntactically (all those let's).
Not sure if there are other things it has in common.
func methodName(paramName: String)
and func methodName(paramName: String, otherParam: Int)
are different.I'm still playing with this but there are some interesting/weird things in here. What follows is a bunch of random observations of how named params work in Swift / ObjC. For instance:
func methodName(name name: String)
Can be shortened using a pound symbol, because the variable name within the method, and the parameter name are the same thing: func methodName(#name: String)
It also seems that most of the Cocoa APIs follow a convention, when called from Swift, of having an unnamed first parameter, and a named second parameter. e.g. this: -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
becomes: func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
which means it's called like so: myObject.tableView(tableView, cellForRowAtIndexPath: indexPath)
If you name a parameter, you have to use the name. The order of parameters matters, and there doesn't seem to be a way to define optional method parameters (make sense as parameter names are essentially ObjC "selectors").Did you miss the part about default values?
If you're calling an Objective-C selector with more than one argument, you are indeed using an external parameter name, but external parameters are explicitly not required if you're writing pure Swift.
https://developer.apple.com/library/prerelease/ios/documenta...
Now, we'll likely not get the Lisp macro system in all its glory, but I think the point to take away (despite the short parent comment) is that its almost better not to have a broken macro system than something that is rushed an not properly implemented.
Are there features in Objective-C available now that weren't present back in the early days? Perhaps this is what the author means? Has Objective-C drawn inspiration from newer languages such as C# or Java?
PS: Apple says "Swift is an innovative new programming language"! Just like everything else they do lately!
It'd be far more interesting to compare it to them (especially where they have bindings for iOS e.g. Java, C#) in terms of productivity, code maintainability, readability etc, instead of in terms of language features.
forall a. SomeClass a => a
in Haskell is quite similar to &SomeTrait
in Rust.interface ISample { void Process(); }
ISample sample = new ConcreteSample(); // Duty #1
class GenericSampleProcessor<T> : where T : ISample // Duty #2
{ public void Process(T sample) { sample.Process(); } }
It's the same in Java:
ISample sample = new ConcreteSample(); // Duty #1
class GenericSampleProcessor<T extends ISample> // Duty #2
Java got generics in 1.5, released September 2004, and bounded types like this were in that release. C# got generics in 2.0, released in November 2005, and i imagine it had them too. I assume Java lifted this idea from elsewhere. I am really quite surprised that Mr Hoare thinks this is a novel discovery in Rust.Link: https://itunes.apple.com/us/book/the-swift-programming-langu...
Hope it helps.
- How about Garbage Collection?
- Many mentions to "type inference", is it then a statically-typed language? Or is it hybrid?
- It is a statically typed language. Type inference is the same mechanism as is used in C++11 with the auto keyword. Basically, anywhere the compiler can "guess" the type, you don't have to be explicit about it. Unless you want the variable to have a different type from the inferred type, that is.
But hey, any opportunity to pimp your favorite language is fair.
It seems to borrow quite extensively C# and .. I don't know how to say this politely or immodestly ... Rust. Which is flattering if true! Of course I'm biased. Also a language pluralist, and since Rust is a major cobbling-together of things we liked in other languages (ML, C++, C#, Lisp, Ruby, etc.)
It also seems like he's more generally excited about the prospects of not being limited to a small set of languages to do powerful things. "It's remarkable to me that in the years between, we've seen such a shift in what's considered "normal" new-language tech. F# is shipping on several platforms (whether or not M# ever actually surfaces again); Scala is considered an employable skill; C++11 has lambdas and local type inference at least, if not algebraic types or pattern matching; Rust actually exists now; and now one can rely on similar comforts in the Apple ecosystem. How delightful!"