What is the practical use of nested functions in swift?

What is the practical use of nested functions? This only makes reading the code more difficult and does not make the specific case easier.

func chooseStepFunction(backwards: Bool) -> (Int) -> Int {
    func stepForward(input: Int) -> Int { return input + 1 }
    func stepBackward(input: Int) -> Int { return input - 1 }
    return backwards ? stepBackward : stepForward
}

Source

+14
source share
4 answers

I think the essence of your question is: why not use a private function instead of an ugly nested function?

Simply put, nested functions can facilitate reading and encapsulation.

Readability

- . . , ( , ). , , !

, , , . ? . , . , .

, 5 , 3 , , , , .

, , , .

, . . , . , , self . , .

, , , :

extractAllHebrewNames() // right to left language
extractAllAmericanNames() // left to right language
extractAllJapaneseNames() // top to bottom language

, printName ( ), , , / . ( . .) .

, " " " ".

, ( -, ).

. .

: ""

func doSomething(){
    nested()

    func nested(){

    }
}

:

func doSomething(){
    func nested(){

    }

    nested()
}

, , ,


:

, self.

. :

Call to method 'doZ' in closure requires explicit 'self.' to make capture semantics explicit 

. ,

class P {
    var name: String

    init(name: String) {
        print("p was allocated")
        self.name = name
    }

    func weaklyNested() {
        weak var _self = self
        func doX() {
            print("nested:", _self?.name as Any)
        }

        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
            doX()
        }
    }

    func stronglyNested() {
        func doZ() {
            print("nested:", name)
        }

        DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
            doZ() // will NOT throw error of: 'Call to method 'doZ' in closure requires explicit 'self.' to make capture semantics explicit'
        }
    }

    deinit {
        print("class P was deinitialized")
    }
}

class H {
    var p: P

    init(p: P) {
        self.p = p
    }
}

var h1: H? = H(p: P(name: "john"))
h1?.p.weaklyNested()
h1 = nil // will deallocate immediately, then print nil after 2 seconds

var h2: H? = H(p: P(name: "john"))
h2?.p.stronglyNested()
h2 = nil // will NOT deallocate immediately, will print "john" after 2 seconds, then deallocates

tl;dr :

  • weak var _self = self self weak.
  • :( .

. ?

+20

.

, , :

func get(_ key: Key) -> Value? {
    func recursiveGet(cur: Node) -> Value? {
        if cur.key == key {
            return cur.val
        } else if key < cur.key {
            return cur.left != nil ? recursiveGet(cur: cur.left!) : nil
        } else {
            return cur.right != nil ? recursiveGet(cur: cur.right!) : nil
        }
    }

    if let root = self.root {
        return recursiveGet(cur: root)
    } else {
        return nil
    }
}

, , . , , . ( !)

recursiveGet () get, ( recursiveGet ).

+1

variable, function :

var myStepFunction = chooseStepFunction(true)

myStepFunction(4)

. , , , , :

func doOperation(operation: String) -> ((Double, Double) -> Double)? {
    func plus(s: Double, d: Double) -> Double {
        return s + d
    }
    func min(s: Double, d: Double) -> Double{
        return s - d
    }
    switch operation {
        case "+":
            return plus
    case "-" :
        return min
    default :
        return nil
    }
}

var myOperationFunction = doOperation("-")?(4, 4) // 0
var myOperationFunction2 = doOperation("+")?(4, 5) //9

- . - . , , plus min, / , .

, clouser , . . , http- , ,

0

( Objective-C), " ". , , . . , , , , .

Swift - , , , .

0

Source: https://habr.com/ru/post/1610409/


All Articles