3 ms·
> if let value = blah Language constructs like that seem wrong to me (it's the "let"). That's just one of my mental blocks against Swift.
by drivingmenuts 12y ago
> if let value = blah
Language constructs like that seem wrong to me (it's the "let"). That's just one of my mental blocks against Swift.
- mikeash 12y agoI see this sort of thing a lot and I still can't understand it. Syntax is, to me, completely trivial. Power through it and get on with life. For an experienced programmer, looking at code should be like that scene in The Matrix where the guy points to a screen full of symbols and says he just sees "blonde, brunette, redhead." It goes the other way too: quite a few people have described disliking Objective-C's weird bracket syntax and much prefer Swift because of that, and I can't understand that either. It's just syntax.
- embwbam 12y agoIt's directly related to how familiar the syntax is. If a syntax is very foreign, it's difficult for your brain to read it, and it is uncomfortable. People don't like being uncomfortable :) The more syntax you learn, the less new forms confuse you.
- mattgreenrocks 12y agoExcept learning the syntax of a new language is trivial compared to, say, learning a language with new paradigms. Don't train your brain to run away from the sight of something that is temporarily uncomfortable. It's intellectually lazy and promotes a passive mindset. It's not like all your programming skills just fall apart once you leave your blub language.
- bunderbunder 12y agoI will say that I've always been uncomfortable with binding new values inside an if-statement's test. As far as syntax goes, it's a good thing that it's possible: I'm assuming here that in Swift every statement is an expression, which is a good thing, and such a feature naturally means that doing this sort of thing should be possible. But stylistically it just feels ugly to me. I don't know if Swift has an equivalent to either of the following, but I'd prefer either of them as a cleaner idiom: Null-coalescing a la C#: self.imageUrl = json["imageUrl"].string ?? "http://example.com/empty-avatar.png"; Pattern matching a la F#: self.imageUrl <- match json["imageUrl"].string with | Some(value) -> value | None -> "http://example.com/empty-avatar.png"
- mikeash 12y agoBoth of those work in Swift too. The ?? operator exists verbatim, and the matching can be done using a switch/case statement. Unfortunately, it is not true that every Swift statement is an expression. The if/let construct is actually a special case. That means that you can't do something like: if (let x = foo) && (let y = bar) { ... Which is too bad.
- drivingmenuts 12y agoI get the binding a value inside an if statement. if (i=3*someFunctionResult()) { ... blah using i ... } makes perfect sense to me. The "let" seems unnecessary and, for lack of a better term, unpoetic. Aside from which, the use of let to define a constant in the middle of an if statement is about as wrong as you can get, in my opinion. Define your constants at the top or in another file, FFs.
- bunderbunder 12y ago'let' doesn't define a constant, it binds an immutable value. The semantic difference is enormous. A constant has infinite lifetime and must be known at compile time. A let-binding has limited scope and can be bound a run-time. In a language like Swift that distinguishes between mutable and immutable variables, a 'let' or 'var' is needed to indicate to the compiler what kind of of variable you're trying to create. Beyond that, it also makes for a cleaner language. Creating a special syntax for let statements that only applies inside the condition for an if-statement (and presumably also loops) just so you can avoid typing a few letters approaches PHP levels of unholy grammar pollution.
- mikestew 12y agoDon't know about OP, but for me it flips the "GWBASIC" switch. I'll admit to my own biases, and one of those biases is based on some design decisions in BASIC. So, right or wrong, when I see old BASIC code I go "ugh". By extension, when I see a language construct that I haven't seen since I last looked at BASIC I also go "ugh". You're right, I need to get over it and I will. But for the time being a little piece of me whinces when I type "let foo = 3". "OMG, should I be numbering my lines, too?"