6 ms·
For people coming from C/C++/ObjC, certain things in Swift can take some time to get used to, e.g. if case .Success(let person) = personResult { ... } My f
by Koshkin 9y ago
For people coming from C/C++/ObjC, certain things in Swift can take some time to get used to, e.g.
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/ https://www.natashatherobot.com/swift-guard-better-than-if/
- yodon 9y agoPlease - tell me that's not something one might actually encounter in practice (and yes I know, it'll feel clever to respond with up votes and down votes but please don't as I'll not have any idea what they mean: good point! Or stupid comment! Or you would totally see this! Or you wouldn't! And for the last two which vote is which?)
- programmarchy 9y agoYou would totally see this in the code I've been writing. I've been taking advantage of enums with associated values for things like state machines. Usually, you'll see associated values like this in a switch statement, but if you need to handle a specific case, then you'll see the `if case`.
- Koshkin 9y agoYou'll be surprised how soon after you start writing Swift you'll be doing this yourself - and enjoy it. Exploring new features and learning new ways of doing things is fun. Otherwise, what's the point in learning a new language? Seeing more of the same?
- pivo 9y agoNot only is this not unusual it is completely normal and even banal when using Swift enumerations with associated values. It might look strange to someone who doesn't know the language, but a few minutes reading about Swift's enumerations will clarify what this code is doing and it will become totally mundane. 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.
- megaman22 9y agoAh, it's an ugly, verbose syntax for pattern matching. Got it :-)
- pivo 9y agoWell, it's very similar to Scala's pattern matching. Not as elegant as Erlang or Haskell's. Objective-C, which is what Swift is being compared to here, doesn't have pattern matching. An attempt to write something similar in Objective-C would be much more verbose, unsafe and almost certainly uglier.
- Grustaf 9y agoYou may find it ugly, that's subjective - I personally find it weird and unintuitive. But you can't really say it's verbose, there is no redundant information there.
- apple4ever 9y agoThat’s the biggest reason I don’t like Swift- it’s terrible syntax. 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.
- Koshkin 9y ago> 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).
- abritinthebay 9y agoI find this fascinating because the main reason I never did iOS development at all (as a hobby) is that I found Obj-C just so goddamn ugly and awful (java level ugly) that I avoided it. Swift changed that completely. I finally find native iOS readable.
- wingerlang 9y agoIf you use it for more than a few hours you just wont think of it anymore.
- pivo 9y agoI write in both ObjC and Swift. It takes more than a few hours to get used to both languages but it’s also true that you get used to both. I prefer the relative terseness of Swift and also find the language more elegant and safer than ObjC.
- abritinthebay 9y agoTried that. Obj-C was just constant nails-on-a-chalkboard annoying. Too verbose, overly clunky syntax, etc. So I’m afraid that wasn’t the case.
- 9y ago
- overcyn 9y agoThis is my personal gripe with Swift. Theres just so many different ways to form expressions and do control flow. It just feels like code golf to me. 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/16/pattern-matching-4/ http://alisoftware.github.io/swift/pattern-matching/2016/05/...
- ricardobeat 9y agoI don't know Swift much, but can tell at first glance that you're mapping items from `mediaList` into (title, kind) tuples, filtering for missing titles and "Harry Potter". I guess it's more about being comfortable with functional programming patterns in general, than its syntax.
- Grustaf 9y agoThis is really the one construction in Swift that I find hard to understand and hence remember, it's not a very fair example.