https://thoughtstreams.io/jtauber/swift-programming-language/Swift Programming Languagejtauber2014-08-06T21:41:11Zhttps://thoughtstreams.io/jtauber/swift-programming-language/#card-4788The postfix `?` (at least after a typ...2014-08-06T21:41:11Z2014-06-03T03:30:51Z
The postfix `?` (at least after a type `T`) is just syntactic sugar for `Optional<T>` which is a `enum` of `None` and `Some(T)` which conforms to the `LogicValue` protocol.
**UPDATE**: but now see <https://thoughtstreams.io/jtauber/new-in-swift-beta-5/>
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4919I just spent a couple of hours debugg...2014-06-15T22:44:30Z2014-06-15T22:31:57Z
I just spent a couple of hours debugging my port of Minilight from Python (itself a port) to Swift.
The bug turned out to be that in the Python version I could test for non-None and non-zero at same time; in Swift I need to test separately.
So my Python was:
distance = triangle.get_intersection(...)
if distance and (distance < nearest_distance):
...
Now distance can be `None` as well as `0.0` and the above code handles both.
My original Swift port, however, neglected the `0.0` case and only handled the `nil` case in the clever type-safe way that `if let` supports:
if let distance = triangle.getIntersection(...) {
if distance < nearestDistance {
...
The fix was to make it:
if let distance = triangle.getIntersection(...) {
if distance > 0 && distance < nearestDistance {
...
I wonder if that experience is actually an argument *for* if clauses only taking explicit booleans (contra what I posted here earlier).
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4810I also wonder to what extent enums ca...2014-06-06T22:25:21Z2014-06-06T04:03:34Z
I also wonder to what extent enums can be used for algebraic data types.
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4809I wonder if an enum with one value is...2014-06-06T04:02:03Z2014-06-06T04:02:03Z
I wonder if an enum with one value is actually a good way to implement singletons.
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4805Building Cocoa apps in Swift is a lit...2014-06-04T20:40:21Z2014-06-04T20:40:21Z
Building Cocoa apps in Swift is a little like what I remember writing Tk apps in Python was like. The biggest challenge is all the documentation is written for a different language binding.
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4792This looks helpful:
[Using Swift w...2014-06-04T11:24:38Z2014-06-04T11:24:38Z
This looks helpful:
[Using Swift with Cocoa and Objective-C](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/BuildingCocoaApps.pdf)
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4791Working through an iOS development tu...2014-06-04T11:23:47Z2014-06-04T11:03:37Z
Working through an iOS development tutorial, I hit a known issue in the first beta of Xcode 6: "Unwind segue actions declared in Swift classes are not recognized by Interface Builder".
I found this: <http://stackoverflow.com/a/24030113/112855>
but still haven't got it working.
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4787One of the interesting things about t...2014-06-03T14:20:19Z2014-06-03T02:00:43Z
One of the interesting things about the `if let` with optionals is you can chain optionals together and handle a nil at any level with a single `if`.
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4784One thing is clear: Swift has a *lot*...2014-06-03T14:19:51Z2014-06-03T01:19:41Z
One thing is clear: Swift has a *lot* of syntactic sugar.
reversed = sort(names, >)
means
reversed = sort(names, { $0 > $1 } )
which means
reversed = sort(names, { a, b in a > b } )
which means
reversed = sort(names, { a, b in return a > b } )
which means
reversed = sort(names, { (a: String, b: String) -> Bool in return a > b } )
which is the same as:
func backwards(a: String, b: String) -> Bool {
return a > b
}
reversed = sort(names, backwards)
and none of that even makes use of trailing closure syntactic sugar yet!
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4776An additional nice thing about the `i...2014-06-03T14:18:06Z2014-06-02T23:23:31Z
An additional nice thing about the `if let` syntax is that methods like `.toInt()` return optionals (rather than raising an exception) so you can do things like:
if let myInt = someValue.toInt() {
// myInt is someValue converted to an Integer
} else {
// someValue is not convertible to an Integer
}
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4774Disappointed that Swift didn't borrow...2014-06-03T14:16:33Z2014-06-02T22:58:29Z
Disappointed that Swift didn't borrow from Go in not needing implementations of a protocol/interface to explicitly declare the fact.
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4769They've gone with:
for (foo, b...2014-06-03T14:15:47Z2014-06-02T22:36:47Z
They've gone with:
for (foo, bar) in baz
meaning `baz` is a dictionary, `foo` the key and `bar` the value.
I guess that's because they don't have lists of tuples and so `baz` couldn't be a list of tuples (like the above would be interpreted in Python)
**EDIT**: I think they *do* have lists of tuples so I guess they just don't have Python's tuple unpacking.
**EDIT 2**: Oh wait, they *do* have tuple unpacking. So how does the above work? Maybe it works one way with dictionaries and another way with lists?
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4786Given the overlaps between classes an...2014-06-03T01:49:39Z2014-06-03T01:49:39Z
Given the overlaps between classes and enumerations, I wonder if the best way to think of enumerations is as classes with a finite, fixed set of instances.
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4785There's a Firefly reference really ea...2014-06-03T01:35:36Z2014-06-03T01:27:07Z
There's a Firefly reference really early on but then on Page 220 of the Swift book there's a Dr Who reference:
println("Lots of planets have a north")
HHGTTG reference on Page 221. Perhaps I should go back and find others.
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4783I like that, in Swift, you don't have...2014-06-03T01:13:50Z2014-06-03T01:13:50Z
I like that, in Swift, you don't have a separate operator function (like `operator.gt(a, b)` in Python) but instead can just pass in `>` where the function is needed.
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4782Is it always possible to write:
...2014-06-03T01:11:03Z2014-06-03T01:11:03Z
Is it always possible to write:
function name(inputs) -> output {
body
}
as
name = {
(inputs) -> output in
body
}
?
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4781Interesting to have `inout` parameter...2014-06-03T01:04:53Z2014-06-03T01:04:53Z
Interesting to have `inout` parameters when you also support functions with multiple return values.
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4780I'm not yet convinced of the value of...2014-06-02T23:59:45Z2014-06-02T23:54:03Z
I'm not yet convinced of the value of external parameter names. I guess they only make sense if you want to keep Objective-C-like naming of named-args without the weirdness that can result from that in the function body.
**EDIT**: Ah, I see. It seems external parameter names are *required* if you want to call the function with explicit names and Swift lets you use a different name for that case than what you want to call it in the function body. There is the `#` syntax if the name in the function body is the same as the external parameter.
**EDIT 2**: In light of this, it seems a little odd that parameters with default values are automatically given external parameters names (without needing to use `#`).
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4779I like that you can label loop struct...2014-06-02T23:49:07Z2014-06-02T23:49:07Z
I like that you can label loop structures and then refer to the label in a `break` or `continue`.
I actually needed that in Python last night.
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4778Looks like `\(...)` string interpolat...2014-06-02T23:34:29Z2014-06-02T23:34:29Z
Looks like `\(...)` string interpolation supports any expression.
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4777Something to watch for:
-9 % 4...2014-06-02T23:28:08Z2014-06-02T23:28:08Z
Something to watch for:
-9 % 4 // equals -1
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4775Ah, I now understand the `if let` thi...2014-06-02T23:19:07Z2014-06-02T23:18:07Z
Ah, I now understand the `if let` thing better.
Optionals evaluate to a boolean so if `foo` is a `String?` then you can say `if foo` to test if `foo` is non-nil.
Now if `foo` is non-nil, you can say `foo!` to get that value (you'll get a runtime error if `foo` is nil).
So:
if let bar = foo {
...
}
is syntactic sugar for:
if foo {
let bar = foo!
...
}
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4773Wait, so the main difference between ...2014-06-02T22:56:20Z2014-06-02T22:56:20Z
Wait, so the main difference between `class` and `struct` is that one is pass-by-reference and the other pass-by-value?
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4772Interesting syntactic sugar: “A closu...2014-06-02T22:49:57Z2014-06-02T22:49:57Z
Interesting syntactic sugar: “A closure passed as the last argument to a function can appear immediately after the parentheses.”
i.e.
foo(...) {...}
instead of
foo(..., {...})
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4771Lexical scoping. Given how often I no...2014-06-02T22:42:00Z2014-06-02T22:42:00Z
Lexical scoping. Given how often I now rely on it, I sometimes forget that Python didn't have it for ages.
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4770There's a syntax for both an inclusiv...2014-06-02T22:38:38Z2014-06-02T22:38:38Z
There's a syntax for both an inclusive and exclusive range. `1..5` means 1 thru 4, `1...5` means 1 thru 5.
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4768Optional values are intriguing: expli...2014-06-02T22:31:51Z2014-06-02T22:31:51Z
Optional values are intriguing: explicitly declaring something to be nullable.
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4767Not sure I'm a huge fan of `if` havin...2014-06-02T22:28:51Z2014-06-02T22:28:41Z
Not sure I'm a huge fan of `if` having to take a boolean (although I understand it's consistent with other decisions). In Python, I like being able to say `if items` where `items` is a list.
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4766Square brackets are used for both lis...2014-06-02T22:26:06Z2014-06-02T22:26:06Z
Square brackets are used for both lists *and* dictionaries. `[]` means an empty list and `[:]` means an empty dictionary.
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4765Swift does Perl-style string interpol...2014-06-02T22:23:39Z2014-06-02T22:23:16Z
Swift does Perl-style string interpolation from locals:
let apples = 3
let appleSummary = "I have \(apples) apples."
https://thoughtstreams.io/jtauber/swift-programming-language/#card-4755[Swift page on developer.apple.com](h...2014-06-02T19:19:49Z2014-06-02T19:05:21Z
[Swift page on developer.apple.com](https://developer.apple.com/swift/)
[Swift Programming Language iBook](https://itunes.apple.com/gb/book/swift-programming-language/id881256329)