LoginSignup
6
7

More than 5 years have passed since last update.

Swiftでローカルスコープ

Last updated at Posted at 2016-02-10

これが正解!

do {
  // スコープ
}

以下間違い

これでとりあえず出来る。

if true {
  // スコープ
}

こういうのの方がカッコいいけど、

func locally(work: () -> ()) {
    work()
}

locally {
    let g = 42
    println(g)
}

C++で言う、

bool hoge() {
  {
    return true
  }
  {
    return false
  }
}

みたいなのが出来ないのが辛い。
オプショナルとジェネリックとかで上手いこと出来そうな気もする。

6
7
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7