Skip to content

Instantly share code, notes, and snippets.

@farble1670
Last active January 17, 2025 20:47
Show Gist options
  • Select an option

  • Save farble1670/862511ffc9be78d98d8ec21ca8f1ec03 to your computer and use it in GitHub Desktop.

Select an option

Save farble1670/862511ffc9be78d98d8ec21ca8f1ec03 to your computer and use it in GitHub Desktop.

If you already know Java and are just looking for a mapping of the Java concept to Kotlin, this will help.

+---------------------+--------------------------------+--------------------------+-----------------------+------------------------+
|                     |                            JAVA                           |                    KOTLIN                      |
|      Pattern        +--------------------------------+--------------------------+-----------------------+------------------------+
|                     |       Declaration-site         |         Use-site         |    Declaration-site   |        Use-site        |
+---------------------+--------------------------------+--------------------------+-----------------------+------------------------+
| Covariant (out)     | class Box<T extends Animal>    | Box<? extends Animal>    | class Box<out T>      | Box<out Animal>        |
+---------------------+--------------------------------+--------------------------+-----------------------+------------------------+
| Contravariant (in)  | Not supported                  | Box<? super Animal>      | class Box<in T>       | Box<in Animal>         |
+---------------------+--------------------------------+--------------------------+-----------------------+------------------------+
| Invariant           | class Box<T>                   | Box<Animal>              | class Box<T>          | Box<Animal>            |
+---------------------+--------------------------------+--------------------------+-----------------------+------------------------+
| Any type            | class Box<T>                   | Box<?>                   | class Box<T>          | Box<*>                 |
+---------------------+--------------------------------+--------------------------+-----------------------+------------------------+

For a deeper dive, see the references linked below.

Reference

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