swift 是否允许没有 conditions/loops 的代码块来缩小局部变量范围?
Does swift allow code blocks without conditions/loops to reduce local variable 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)
}
}
当我在 Swift 中执行此操作时,它认为我正在创建闭包并且不执行代码。我可以将其创建为闭包并立即执行,但这似乎会带来执行开销(不值得仅仅为了代码清洁)。
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)
})()
}
在Swift中是否支持类似的东西?
您可以使用 do
语句在 Swift 中创建任意范围。例如:
func foo() {
let x = 5
do {
let x = 10
print(x)
}
}
foo() // prints "10"
根据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
@Jack Lawrence 的答案的替代方法是使用积木;类似于您的第一个代码片段中的块。
func foo () {
let x = 5
let block = {
let x = 10
print(x)
}
block()
}
foo()
在具有块级作用域的语言中,我有时会创建任意块,这样我就可以封装局部变量,而不是让它们污染它们父级的作用域:
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)
}
}
当我在 Swift 中执行此操作时,它认为我正在创建闭包并且不执行代码。我可以将其创建为闭包并立即执行,但这似乎会带来执行开销(不值得仅仅为了代码清洁)。
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)
})()
}
在Swift中是否支持类似的东西?
您可以使用 do
语句在 Swift 中创建任意范围。例如:
func foo() {
let x = 5
do {
let x = 10
print(x)
}
}
foo() // prints "10"
根据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
@Jack Lawrence 的答案的替代方法是使用积木;类似于您的第一个代码片段中的块。
func foo () {
let x = 5
let block = {
let x = 10
print(x)
}
block()
}
foo()