7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swiftのジェネリクス型を継承したときの挙動

Last updated at Posted at 2015-01-03

ジェネリクス型を継承ときに、継承したクラスで親のジェネリクス型を記述できないかと思ったのですが、以下のように記述してもコンパイルエラーになってしまいました。

class Foo<T> {

}

class Bar : Foo<Int> {

}

以下コンパイルエラー文です。

Classes derived from generic classes must also be generic

継承したクラスでもジェネリクスを定義する必要があります。

class Foo<T> {

}

class Bar<T>: Foo<T> {

}

毎回ジェネリクス型を明示するのが面倒な場合はtypealiasで定義してエイリアスで記述することもできます。

typealias BarClass = Bar<Int>
7
7
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?