
Swift Function Fun Facts - DaNmarner
http://dduan.net/post/2015/04/swift-function-fun-facts/
======
gilgoomesh
There's more than a couple situations where Swift forces you to repeat
yourself. No default implementations for protocols and missing language
features for dealing with enums would probably be more prominent examples than
this narrow availability of default arguments problem.

I'm not sure any one of these "work in progress" related issues are a reason
to "hate Swift" (or at least, where Swift is going). It's more a reason to
hate the current status of Swift as a project – since it will probably be a
year or two before these sorts of non-critical issues bubble to the top of the
Swift development team's priority list.

------
eridius
This is a very odd post. It basically boils down to default arguments only
work on named functions, they don't work on function values (whether this is a
curried function, or a declared value of function type). Which seems like a
perfectly reasonable restriction to me; default argument values are a property
of named functions, not of function types, and cannot be expressed in the type
system. While it's not out of the realm of possibility for Swift to extend the
type system to support this, it's not something I'd hold my breath for and
it's not something I feel that I could rightly criticize Swift for not having.
And yet, the author declares "Now, you hate Swift" based on this one perceived
limitation.

~~~
nwg
> default argument values are a property of named functions, not of function
> types, and cannot be expressed in the type system.

For what reason? Isn't the gripe exactly with the fact that it's not
implemented?

Also why are arguments required to be named in all calls after the first, but
not the first?

func f(a: Int)(b: Int)(c: Int) -> Int { return a + b + c }

let v = f(2)(b: 5)(c: 5)

Stuff like this just eats at me when i use a language, especially when that
language is brand new and other languages have had nice syntax for similar
features for a long time.

~~~
eridius
> For what reason? Isn't the gripe exactly with the fact that it's not
> implemented?

How should it be represented in the type system? There's no clear answer to
that question.

The type of a function is `(Args) -> Ret`. `->` is basically an infix type
constructor with two type parameters; it's basically sugar for something along
the lines of `Func<(Args),Ret>` (except there's no actual type named that).
The arguments list here is actually just a tuple, and functions with named
arguments have an arguments list which is a tuple with named fields (since
Swift supports that). Given all this, I don't see how default parameters could
be crammed in. At a syntactic level, you could say something like `(a: Int =
default, b: String = default) -> String`, but when you realize that the
arguments list is a tuple, a "tuple with default field values" is a nonsense
thing to try and construct.

Also, more generally, a function with default parameters is actually a family
of functions, one for each valid combination of parameters. But a function
value is a single function, not a family of functions. When you think about it
like that, trying to return a function value with default parameters makes no
more sense than trying to return a generic function that hasn't been
instantiated with concrete type parameters (e.g. you cannot say `func foo() ->
(<T>(x: T) -> T)` or anything along those lines). Function values must be a
single concrete function, not a family of functions.

------
alexjarvis
I also ran into this problem a couple of times..

It's not the only issue. I put it down to how new the language is and how many
non-critical issues they can fix.

Swift 1.2 has been a massive improvement with incremental compiling and the
SourceKit HUD not flickering in my face every few minutes. But still Xcode
crashes frequently.

With all the known problems considered, I still find that the advantage of
using Swift far outweighs the small number of issues with it in its current
version.

------
dilap
headers are string -> [string]!

