Does swift allow code blocks without conditions/loops to reduce local variable scope? [duplicate]

妖精的绣舞 提交于 2020-01-03 16:47:13

问题


In languages with block level scope, I sometimes create arbitrary blocks just so I can encapsulate local variables and not have them pollute their parents' scope:

func myFunc() {
  // if statements get block level scope
  if self.someCondition {
    var thisVarShouldntExistElsewhere = true
    self.doSomethingElse(thisVarShouldntExistElsewhere)
  }

  // many languages allow blocks without conditions/loops/etc
  {
    var thisVarShouldntExistElsewhere = false
    self.doSomething(thisVarShouldntExistElsewhere)
  }
}

When I do this in Swift, it thinks I'm creating a closure and doesn't execute the code. I could create it as a closure and immediately execute, but that seems like it would come with execution overhead (not worth it just for code cleanliness).

func myFunc() {
  // if statements get block level scope
  if self.someCondition {
    var thisVarShouldntExistElsewhere = true
    self.doSomethingElse(thisVarShouldntExistElsewhere)
  }

  // converted to closure
  ({
    var thisVarShouldntExistElsewhere = false
    self.doSomething(thisVarShouldntExistElsewhere)
  })()
}

Is there support for something like this in Swift?


回答1:


You can use a do statement to create arbitrary scope in Swift. For example:

func foo() {
    let x = 5

    do {
        let x = 10
        print(x)
    }
}

foo() // prints "10"

As per The Swift Programming Language:

The do statement is used to introduce a new scope and can optionally contain one or more catch clauses, which contain patterns that match against defined error conditions. Variables and constants declared in the scope of a do statement can be accessed only within that scope.

A do statement in Swift is similar to curly braces ({}) in C used to delimit a code block, and does not incur a performance cost at runtime.

Ref: The Swift Programming Language - Language Guide - Statements - Do Statement




回答2:


An alternative to @Jack Lawrence's answer is to use blocks; similar to the block in your first code snippet.

func foo () {
    let x = 5

    let block = {
        let x = 10
        print(x)
    }

    block()
}

foo()


来源:https://stackoverflow.com/questions/39109651/does-swift-allow-code-blocks-without-conditions-loops-to-reduce-local-variable-s

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!