Search code examples
swiftios8swift-playground

Not able to use recursion in swift


I am using this function

    func countFrom(from:Int, #to:Int) -> () {
        println("\(from)")
        if from < to {
             countFrom(from + 1, to: to)
        }
    }
    countFrom(1, to: 10)
}

But on compiling i get Swift Compiler Error -

Command /Applications/Xcode6-Beta2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 1

And if i removed "countFrom(from + 1, to: to)", then there is no more error. What's wrong with that?


Solution

  • Answer: the code is fine, the compiler is outdated. Per @Matt's comment, this works fine in Beta 4.

    Console Output:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10