if case .Success(let person) = personResult { ... }
My first thought when I saw this was, "only a mother could love this syntax", but later you come to appreciate and enjoy it.Link: https://www.natashatherobot.com/swift-guard-better-than-if/
if case .Success(let person) = personResult { ... }
My first thought when I saw this was, "only a mother could love this syntax", but later you come to appreciate and enjoy it.Link: https://www.natashatherobot.com/swift-guard-better-than-if/
for case let (title?, kind) in mediaList.map({ ($0.title, $0.kind) }) where title.hasPrefix("Harry Potter") {
print(" - [\(kind)] \(title)")
}
http://alisoftware.github.io/swift/pattern-matching/2016/05/...I hate let, var, func. I hate the backwards variable definitions. I hate the crazy punctionation symbols (dots, question marks).
I agree with a lot of what Smith says about Objective-C. But my biggest personal reason is the above. I find Objective-C beautiful and Swift ugly.
That's subjective. Unusual - yes, but ugly? Why? I'd say, it is quite beautiful, in its incomprehensibility (to the untrained eye).
Swift changed that completely. I finally find native iOS readable.
So I’m afraid that wasn’t the case.
It’s not - thank god - so I don’t have to. So I won’t.
It’s ugly for a reason but that still means it’s ugly.
I can see why people might thing square brackets are ugly, though I prefer it to the double-colon and angle bracket fest that is C++, but what else do you find uniquely ugly about Objective-C? Infix operators are nifty and promote clarity.
Yes it expresses a subjective opinion.
Programmer preferences on languages, tooling and other things involve subjective opinion. We are all humans, not purely rational automatons.
There is nothing wrong with a purely subjective, and taste driven argument. How much weight you choose to give it is another matter.
> I hate let, var, func
For me it increases readability, for example when I see let, I can make assumptions about it not being mutated, which is a huge win in a large codebase.
> I hate the backwards variable definitions.
To me it says, "this is a variable called x of type String", which seems to make sense to me, definitely more so that the C way.
I hate the crazy punctionation symbols (dots, question marks).
Question marks denoting optionals seems very clear, like "is String? really there?"
> I find Objective-C beautiful and Swift ugly.
See, I am the other way, I don't really find [[]] all that beautiful.
What we know is that this won't work:
let p = Person("one")
p = Person("two")
But we don't know whether this will work: p.name = "three"
It depends on whether Person is a struct or a class, but that information is not available locally. let/var is fine, but in terms of information content it is strictly worse than what const can do in C.> in terms of information content it is strictly worse than what const can do in C.
True, however the Swift community largely adopted a "use let first, only use var if absolutely necessary" approach, which is strictly better in terms of mutability-related bugs than what it was in ObjC/C, as mutability there is used much more liberally than in Swift.
struct Person {
var name: String
}
enum Result<T> {
case success(T)
case error(NSError)
}
func printName(personResult: Result<Person>) {
if case .success(let person) = personResult {
print(person.name)
}
}
In the example above, Result is an enumeration that can be either success or error. If it's success it has an associated value that is the result of whatever operation was successful. If it's error the associated value contains the error information.In the case of printName() the success case contains a Person value. The if-case is simply saying that if the enumeration represents the success case then extract the associated Person value from the enumeration. The "let person" means make person a constant because you're not intending to modify it. If you wanted to modify it you'd use "var person" instead.
Hopefully this is pretty clear even to people not familiar with Swift style enumerations.