Multiple trailing closures fit a common use case and seem like a natural extension of the language.
Absolutely.
But Swift wasn't parsimonious at first. Not even close. It unwisely rejected the parsimonious case and chose tremendous complexity.
Keywords? Or parens? Both! And we'll throw in a bunch of other stuff as well. And the weird case of having two keywords, one with a colon and one without, because we decided to make the keywords the names of the parameters. Sometimes.
And then afterwards they noticed that the parsimonious choice that they had initially rejected in favor of their much more complex choice was needed in addition to all that initial complexity.
> Keywords? Or parens? Both!
Why should they be mutually exclusive?
Swift's named parameters are great! For instance they allow for default parameters, which is something I really miss in Rust for instance where I have to use the builder pattern everywhere.
> And the weird case of having two keywords, one with a colon and one without, because we decided to make the keywords the names of the parameters. Sometimes.
Swift's rules for parameter naming are very simple, consistent, and easy to understand. Given the signature:
func foo(first second: Type)
`first` is the name used outside of the function, or the keyword for this argument, and `second` is the name used inside the function, or the parameter name. If the two names are the same, the second name can simply be elided. So for instance this signature: func foo(x: Int)
Is just syntactic sugar for this: func foo(x x: Int)
It's super simple and something which every swift developer can pick up within minutes of starting their first tutorial. I don't really understand why you seem to getting so agitated over this non-issue.Yes, regretfully.
>> Keywords? Or parens? Both!
> Why should they be mutually exclusive?
Because of parsimony. The comment I was replying to made the claim that the original Swift design war parsimonious. It wasn't.
> Swift's named parameters are great!
Named parameters are great. Swift's implementation of named parameters is deficient, and not parsimonious. Yes, even that design is better than not having named parameters, but once you have named parameters, you don't need the parens.
Can you share an example of what this would look like in practice? I'm having trouble thinking of how this syntax would work.
var a, b, c;
...
a = b.fooBar(1, 2);
c = a.fooBarBaz(1, 2, a);
vs |a b c|
...
a := b foo:1 bar:2.
c := a foo:1 bar:2 baz:a.
...
From: https://live.exept.de/doc/online/english/programming/stForJa...The Smalltalk syntax is hard for me to parse, but it's probably just a familiarity thing. I remember when I first learned Swift I wasn't fond of not ending lines with semicolons. Now I dislike having to type them in languages where they're required.
1 to: 20 do:[ :i | Transcript show:i. ]
or in Obj-S 1 to: 20 do:{ stdout println:$0. }
The keyword syntax makes a LOT of other syntax unnecessary, and allows you to create APIs that read like DSLs.While binary and unary messages chain w/o problems, it's not possible to disentangle two keyword messages:
receiver msg1:arg1 msg2:arg1
is indistinguishable from receiver msg1:arg1 msg1:arg2
So ST-80 chooses the latter. If you mean the former, you have to parenthesize.But not always, it turns out. If you actually chain messages to the same receiver, you can use the semicolon to chain those messages:
myRect x:20;
y:10;
width:100;
height:100.
This looks like the single message x:y:width:height: but is actually four separate messages, all sent to myRect. It makes additional compound messages, particularly setters, less necessary.In Objective-S[1], you can use the pipe ("|") to chain message-sends that go to the result of the previous message, rather than its receiver. For example:
self dictionariesForQuery: "select {column} from {table}" | collect | at:column.
Since Objective-S also supports dataflow, the similarity to Unix pipes is intentional, and it pretty much works semantically as well: the result of the previous expression gets piped into the next expression. Very intention-revealing. In fact, I've found it to be so useful in communicating the structure of expressions that I use it even when it's not strictly necessary, for example the second instance in the example above.> delineating the argument set for a single function is the main job parens fulfill.
Right. Which is one of the many reasons why the case of Swift is so mind-boggling: there some of the parameters go inside the parens, some outside. I think you can make a good case for either all in or all out (better for all-out, IMHO, but whatever). But partly in and partly out?
Swift's keyword syntax isn't complex at all.
> Keywords? Or parens? Both!
I know you like Smalltalk, but keywords and parens are used together in a lot of languages (e.g. Python) so it's a tested idea.
> And the weird case of having two keywords, one with a colon and one without, because we decided to make the keywords the names of the parameters. Sometimes.
You don't have two keywords, you have an optional variable name. It's intuitive at the first time you see it - you're phrasing makes it seem much more complex than it really is.
I think you're trying to imply that Swift isn't an elegant language with it's syntax fitting in a postcard, but that doesn't mean it's 'tremendous complexity'. This whole comment is a bit too harsh to Swift, calling the legitimate differences between Swift and Smalltalk as 'complexity'.
> Swift's keyword syntax isn't complex at all.
Yes it is, particularly compared to the parsimonious options. Either parens or Smalltalk-style keywords can do the job, so you absolutely, positively do not need both.
Particularly, if you are going to have keywords, you just don't need parens. So it's already more complex than it needs to be (not parsimonious). Just explaining the various omitted or extra keywords takes 4 pages in the Swift book, the entire Smalltalk syntax fits on a Postcard.
What is complex or not obviously depends on what your baseline is. If it's C++, Swift might seem simple (though many disagree).
> (e.g. Python) so it's a tested idea.
The question was not whether it was tested, but whether it was a parsimonious design, which it is not. It was also not a good design, because it led to a number of follow-on problems that they had to incrementally fix while adding even more complexity.
Python is not a good comparison because they didn't really have a choice: they already had the paren syntax, which they could hardly deprecate.
However, choosing that design without a backwards compatibility restriction is unconscionable, IMHO, particularly if you already have a different keyword design all over your code-base. That's cutting off your nose to spite your face.
Anyway, even the baseline keywords-in-parens is complex and more complex than it needs to be (because it isn't parsimonious), but then you get follow-on problems: blocks inside parens are really ugly, so you add trailing closure syntax. Yet another special case, which is very different from arguments inside the parens. But then you discover that sometimes you need more than one trailing closure, and then you need yet another special case, the open-keyword-trailing-block syntax, which is different yet again.
Of course, instead of having 3-4 different syntactic mechanisms, you could have chosen just the one and not needed any of the others.
> You don't have two keywords, you have an optional variable name. It's intuitive at the first time you see it
No, it's absolutely positively not the least bit intuitive. And it makes no sense, because internal variable names have no business leaking to the interface. If you want to rename the names of your variables inside your method, it changes the signature! Not even C does this.
> that doesn't mean it's 'tremendous complexity'
Although the argument could be made, I wouldn't, not by itself, no. However, this is just one example of countless others, see for example Rob Rix, whom I quoted before:
Swift is a crescendo of special cases stopping just short of the general; the result is complexity in the semantics, complexity in the behaviour (i.e. bugs), and complexity in use (i.e. workarounds).
I also talk more about it here:
https://blog.metaobject.com/2015/05/i-am-jealous-of-swift.ht...
> is a bit too harsh to Swift,
Nope. The comment I replied to made the indefensible claim that Swift was parsimonious. Which it's not. Not even close.
I do agree in general that Swift can benefit from a reduction of special cases, but my personal feeling is that Swift is getting more consistent every version (at least in the features that already existed). YMMV, of course.
How so? That is literally what they did. The trailing closure syntax is Smalltalk keyword syntax, and so Smalltalk keyword syntax is used for that one special case. Which exists in addition to the non-keyword trailing closure syntax and the non-trailing (regular argument) closure syntax.
> but it's not fair to say Swift 'added Smalltalk syntax to address a special case'.
Can you explain how it is "unfair" when that is exactly what happened? Smalltalk syntax is really compact, there just isn't that much there.
Exactly. Which is why I wrote "they adopted the syntax", not "they adopted the ideas". It would have been really wonderful had they adopted the ideas, and it would also have made their marketing slogan "Objective-C without the C" actually truthful.
> the ideas are different
Yes, and that's the problem.
> Trailing closure syntax is basically 'you can remove the parens if there are trailing closures'
Not really, no. The parens are still all there, you just move one (original trailing closure syntax) or more (new keyword trailing closure syntax) of the parameters outside the parens.
Which is horribly inconsistent just by itself. I mean, the point of the parens is that they delimit the arguments. If you have parens, then the arguments go inside the parens. Except for this one...did I say one, I meant these two special cases where the arguments do not go inside the parens.
If you are going to have some of the arguments be outside the parens, why not just go all in and have all of them "outside" by just getting rid of the parens and be (a) consistent and (b) dramatically simpler and (c) readable?
> which was inspired by Objective-C, which was inspired by Smalltalk.
> I don't think Smalltalk influenced that syntax any more ...
Which is it?
Anyway, the point of the article wasn't that they consciously decided to adopt Smalltalk syntax. The point is that they bungled this so badly that they ended up adopting the very Smalltalk syntax that they initially rejected, and the reason they ended up adopting it was that they couldn't make things work reasonably any other way. Except that had they done this from the beginning, they would have not run into any of these problems.
https://www.quora.com/Which-features-overcomplicate-Swift-Wh...
That was 2015, when Swift was far simpler than today.
Heck, they added almost all of Smalltalk syntax recently to address one special case of a special case.
https://blog.metaobject.com/2020/06/the-curious-case-of-swif...
And the method syntax that this is a special case of a special case of is many pages of spec.
And of course there's initialization, which was 14 pages of the Swift spec at the time, with crazy overlaps and gotchas. Unsurprisingly and predictably (as in: I predicted it), it wasn't enough, or rather far too specific, so for SwiftUI they had to create an entirely new way of creating/initializing: function builders.
https://blog.metaobject.com/2020/04/swift-initialization-swi...