Resolving “Variable used within its own initial value” Error in Swift

While experimenting with a few things today, I experienced this compiler error:

Variable used within its own initial value

Let me describe the situation I was in…

I was playing (in a playground, no less) with closures, trying to mimic some behavior that I’ve recently learned about in Scala.  Essentially, I was trying to implement a factorial function as a closure, and I was trying to do it recursively (that is, without using a for/while loop).  Here’s what I wanted to do:

If you’ve seen factorial before, the above implementation isn’t new.  The “base case” that will let the recursion stop is the expression if (n == 0), and the recursive case is in the else block, where factorial gets called again within its own body’s definition.  Only problem is… this doesn’t work in Swift 1.0.

Apparently, the closure (which is being initialized and assigned to the constant named “factorial”) hasn’t had a chance to fully initialize itself before the name factorial is used within the body.

The frustrating part is that I really didn’t want to type the letters v-a-r to implement my solution.  But alas, as Stack Overflow says, the following solution to the “initial value” error works:

Of course, there’s absolutely no reason for the implementation to be a closure – I was simply experimenting.  Here’s the solution that I actually prefer… a good ole named function definition: