Help us understand the problem. What is going on with this article?

[Swift] お前、rethrowsだったのかよ...

More than 1 year has passed since last update.

こういう関数があったとしましょう。

func hoge() throws -> Bool {

   // それほど時間のかからない処理
   return result
}

func fuga() throws -> Bool {

  // 結構時間のかかる処理
  return result
}

この2つの関数のうちのいずれかが true なら特定の処理をするとします。

普通に書くとこうなります。

do {
  if try hoge() || try fuga() {
    // 処理
  }
}
catch {}

swiftcさん

error: 'try' cannot appear to the right of a non-assignment operator

だめだー!

書き直します。

do {
  if try hoge() {
    //処理
  } else if try fuga() {
    // 上と同じ処理
  }
}
catch {}

ありえねぇ!!!

書き直します。

do {
  let isHoge = try hoge()
  let isFuga = try fuga()
  if isHoge || isFuga {
    // 処理
  }
}
catch {}

不要かもしれないのに必ず時間のかかるfuga()を実行してるのつらい。

実はこのように書けます。
@t_ae さんにコメントをいただきさらに簡略化しました)

do {
  if try hoge() || fuga() {
    // 処理
  }
}
catch {}

|| さん、 お前、rethrowsだったのかよ...

masakihori
非えんじにあ / 素人が雰囲気で書いてます
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした