Swift: Commonly Rejected Changes
github.com
github.com
> Replace {} Brace Syntax with Python-style indentation: Surely a polarizing issue, but Swift will not change to use indentation for scoping instead of curly braces..
I'm happy this won't happen. As much as I like Python the scoping based on spaces messes me up far too frequently and I yearn to be able to use braces to define my blocks. No idea if I'm in the minority or majority here but I like the explicitness of my braces :)
You can have similar editor support for indentation syntax, where the editor shows scope markers, code folding, scope coloring, etc. in a particular style chosen by the programmer. The editor could even show you the matching beginning construct after you deindent. The important difference is that redundancy in the user interface can be separated from redundancy in the language's grammar, and personal preference can be used to determine the former. The "mash closing braces" scenario you refer to is generally eliminated with indentation syntax; a single deindent can be the equivalent to multiple braces.
<> is next to shift on the keyboard, {} is AltGr+7/9.
The problem is parsing complexity, they have to be disambiguated from the corresponding operators (also the right shift `>>`), so either the syntax has to be such that they can't appear in the same context and can be trivially differentiated, or you got yourself an exponentially harder to parse language.
With the same right one could argue that {} can't be used for generics etc. since most people also have to write JS from time to time and it will confound the hell out of their language parser when switching languages.
Er… mate I'm talking about actual parsers here, as in the stuff that follows the tokenizer…
foo<bar<1> could be interpreted as "foo<(bar < 1)>" or "foo < bar<1>"
It's undecidable without symbol lookup.
It's not like experienced Python programmers are constantly getting confused by their IDEs and wondering what scope a line is in.
There is almost no magic, which is good when learning new stuff. The actual computer is sort of still there. The IDEs are free and good. The environment and APIs are really stable so 10 year old code compiles and runs, and the syntax is traditional. It's not a toy language so it's useful too.
The major drawbacks is that there are a lot of crappy "enterprisy" code out there.
I really liked learning Scheme though (in 1994 or so). I think every php- and javascript-damaged kid really needs a big dose of Scheme, just as we needed it to heal some of the damage that came from Basic and C. (COBOL seems to be very hard to cure)
Clojure does not strike me as anywhere near as "clean" as Scheme, but maybe it's an alternative.
Perhaps a good first year should include courses that uses Java, Scheme and Prolog?
First semester you have 3 courses going into programming from 3 different angles:
- Racket – functional programming, algorithms, etc
- DLX processor design and logical circuits (ended with ARM assembler) – the technical side
- Math – obviously, the pure mathematical side of it
Then, second semester, we did Java, C, complex algorithms and data structures, low-level programming, etc.
I’d say this is a nice way to go at it, because after the first year you have a rough overview how everything, from electron level to malloc, from simple algorithms to a webserver, works.
While I tend to write code to match whatever style guide I am following, I know a few programmers who write extremely messy, poorly indented code and then just hit the magic Clang-auto-format hotkey every now and then to clean up their file. In a whitespace significant language it is not possible to infer the correct indentation if not explicitly provided.
Also if you have unmatched braces, that's generally a trivial parse error - the compiler (or linter) will check that and find that every time. But with indents, if the outcome of some code refactoring accidentally broke the indent and everything after some point is over-indented, that tends to superficially work (I've actually made that mistake several times in coffeescript).
Additionally, diffing tools I know of can be set to ignore whitespace, which makes common code changes much easier to understand. If, on the other hand, you do that with an indent-sensitive language you're going to miss indent-changes entirely. That's just a practical tooling problem - you could write a diff that works just as well for indent-based languages - but it's a practical problem nonetheless.
All in all, I think it's a much better idea to have braces. If you think they're ugly, use your syntax highlighter to deemphasize them. A smart code editor could even omit the braces entirely in normal, syntactically valid code (which I'm not suggesting is necessarily a good idea, but it's possible).
You only need to fix indentation if it doesn't match the block structure. Which modern languages solve by removing the unnecessary* {} tokens.
* If you can remove it, and it still works, it's unnecessary.
There are elements in source code whose sole purpose isn't only to express instructions to the computer. Expressing the programmer's intent to future readers of the source is obviously also a frequent goal. Comments. Extra parentheses that are not strictly required due to order-of-operations rules. Various annotations and attributes. I mean, these are not exactly new, revolutionary ideas.
All the things that you mention exist to help the user: that's what indentation's original purpose was, since parenthesis were obviously not good enough. Having the computer respect the same rules humans use is logical and helps remove the chance for inconsistencies.
In a language with a minimally redundant surface syntax you would still get an error if you use an unbound variable or call a function with arguments of the wrong type. The redundancy here helps tremendously for generating diagnostics. It's not even clear how you would eliminate it, because even determining redundancy (e.g. does a given type have a unique inhabitant modulo reduction) is undecidable in most practical settings.
Beyond that, a lot of the surface-level syntactic redundancy comes from the desire to match convention, e.g. using actual words as keywords and imitating mathematical notation.
Nobody was proposing sending raw instructions to the CPU. Instead, the higher level things, like how we indent blocks, should not be repeated. I believe you already know this.
Just my 2 cents
I think Haskell took an interesting method to the distinction, just use both. (Spaces are translated to the other form automatically)
if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
goto fail;
goto fail;
IMHO if the indentation of the code does not match the brace structure, that is highly suspicious and often very difficult to spot by manual code review, so it should not compile without any warning or error, but I'm not aware of any C-style syntax language implementation that does that. if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
goto fail;
Write if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0) {
goto fail;
}Not so much for me, but I like the rejection because consistency is more important. If I have a language where braces is used exclusively in one part and not the rest, I feel that's a bad design approach. At least in the case of Python, braces is a humor joke.
"SyntaxError: not a chance"Or write thin bindings exposing a C API to the bits of LLVM you need to invoke to drive the backend. I believe that's what Rust does[0], because you definitely can't import C++ in Rust, rustc is written in Rust, and yet it uses LLVM.
[0] might be in https://github.com/rust-lang/rust/tree/master/src/rustllvm though I'm not quite sure
http://llvm.org/docs/doxygen/html/group__LLVMC.html
It's less full-featured than the C++ API, though, and may not help Swift all that much. Swift interoperates seamlessly with Objective-C, but its C FFI requires a lot of awkward boilerplate to translate between types & calling conventions.
LLVM can also parse serialized bitcode, so one option (in any language) is always to dump to serialized LLVM bitcode and then pipe it to llc on stdin. Performance will suffer, though, since you have to serialize & deserialize the bitcode.
I wonder what these significant tradeoffs are? if/else and switch not being expressions makes certain code really awkward to write. Luckily Swift doesn't force you to use mutability here, but it's still weird IMO.
Exactly which statements should be included (certainly if and switch, any others)?
Further, it is important to consider whether the code written using this will actually be better than the code written with these things as statements. For example, the “switch” blocks tend to be very large, and turning them into expressions encourages additional indentation. Swift already allows ‘let’ values to be initialized on multiple paths, so is the win actually that great?
Given that statements-as-expressions would provide another way to do things (they are a purely syntax extension) the barrier should high to add them. They will add complexity and surface area to the language, so they need to pay that complexity.
[0] https://lists.swift.org/pipermail/swift-evolution/2015-Decem...
I would argue that allowing 'let' values to be initialized on multiple paths is uglier than having them be always initialized by exactly one expression. But, I see their point.
Some languages restrict where block expressions can be placed, but that's not a meaningful difference between if expressions and ternaries, that's a difference how blocks are treated. More concretely: I know of no language that allows "if expressions" that aren't just ternaries (i.e. either the language allows blocks as expressions or it doesn't, but given that choice, no language then further distinguishes between if expressions and ternaries).
If I were to catch an exception, I rather catch it at a line level in a block.
If you read through https://mail.python.org/pipermail/python-ideas/2015-Septembe... (a freakin long debate) you will see that sometimes simplifying things can hurt, although in this very debate if/else expression is favor over the proposed new syntax in that PEP, which I am against.
If Swift's idea was to be a C-like language then it doesn't make any sense to suggest indenting as code-block markers, or to not use 'default' in switch statements
And I must say I _must_ prefer the stance Swifts developers take compared to Go's situation. It is because of this that I think that Swift likely has a great future competing with Go in the space of web-software; be it a bit in the long run as it currently lacks some basic facilities/libraries.
I wish the both the best, for 2016 and beyond. But I keep a eye on Swift's web-tech libs as I expect it to blow up in the coming years :)