0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SwiftでVoidを返す

Posted at
% swift -v
Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-darwin23.5.0

Swiftでは、Void型は空のタプルのタイプエイリアスとして定義されています(Void | Apple Developer Documentation)。

typealias Void = ()

明示的にVoidを返したい時ってありますよね。自分はありました。このような場合は、直接空のタプルを返すことができます。

func v1() -> Void {
  return Void()
}
func v2() -> Void {
  return () // 空のタプル
}

具体的には、Result<Void, Error>を返したい場合に役立ちます。

return .success(Void())
return .success(()) // .successの引数に空のタプルを渡す
0
0
1

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?