Skip to content

Instantly share code, notes, and snippets.

@KisaragiEffective
Created May 26, 2020 11:07
Show Gist options
  • Save KisaragiEffective/e3ac338cd8f9c17940b533767156fbc3 to your computer and use it in GitHub Desktop.
Save KisaragiEffective/e3ac338cd8f9c17940b533767156fbc3 to your computer and use it in GitHub Desktop.

1: JavaはIntersection typeやUnion typeを(部分的に)サポートしている

Hoge & Piyo hogePiyo = (Hoge & Piyo) fuga;
// TはCloneableを実装していてSerializableも実装しているクラス
class Hoge<T extends Cloneable & Serializable> {
// ...
}
try {
  // ...
} catch(IOException | NullPointerException e) {
  // ...
}

Intersection typeは二項目以降がインターフェースでなければならないのに対して、Union typeはそのような制限はない。 ただし、Intersection typeやUnion typeはメソッドの返り値にすることはできない。

2: CRTP Curiously Recurring Template Pattern.

interface Hoge<@CRTP T extends Hoge<T>> {
  T returnSubClass();
}

java.lang.Enum<E extends Enum<E>>などで用いられている。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment