{"id":23902864,"url":"https://github.com/mtumilowicz/java11-intersection-type","last_synced_at":"2025-08-12T13:07:12.059Z","repository":{"id":110876861,"uuid":"170003569","full_name":"mtumilowicz/java11-intersection-type","owner":"mtumilowicz","description":"Simple example of intersection types.","archived":false,"fork":false,"pushed_at":"2019-02-13T21:03:17.000Z","size":62,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-05-27T03:04:26.840Z","etag":null,"topics":["intersection-type","intersection-types"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mtumilowicz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-02-10T17:13:49.000Z","updated_at":"2019-02-13T21:03:18.000Z","dependencies_parsed_at":"2023-05-16T04:01:27.520Z","dependency_job_id":null,"html_url":"https://github.com/mtumilowicz/java11-intersection-type","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mtumilowicz/java11-intersection-type","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtumilowicz%2Fjava11-intersection-type","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtumilowicz%2Fjava11-intersection-type/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtumilowicz%2Fjava11-intersection-type/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtumilowicz%2Fjava11-intersection-type/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mtumilowicz","download_url":"https://codeload.github.com/mtumilowicz/java11-intersection-type/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtumilowicz%2Fjava11-intersection-type/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270065441,"owners_count":24520946,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-12T02:00:09.011Z","response_time":80,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["intersection-type","intersection-types"],"created_at":"2025-01-04T22:50:55.770Z","updated_at":"2025-08-12T13:07:12.051Z","avatar_url":"https://github.com/mtumilowicz.png","language":"Java","readme":"[![Build Status](https://travis-ci.com/mtumilowicz/java11-intersection-type.svg?branch=master)](https://travis-ci.com/mtumilowicz/java11-intersection-type)\n\n# java11-intersection-type\n\n# preview\nSince Java 8 we have a new type: **intersection type** that is an intersection (or subtype) of multiple types.\n\nFor more specific info please take a look at: https://docs.oracle.com/javase/specs/jls/se11/html/jls-4.html#jls-4.9\n(quite complex and obscure description).\n\n# project description\nWe will show easy example where intersection types are very handy:\n1. Suppose we have classes from third-party libraries (we can't modify them):\n    ```\n    class Impl1 implements AutoCloseable, Runnable {\n    \n        @Override\n        public void close() {\n            System.out.println(\"closed1\");\n        }\n    \n        @Override\n        public void run() {\n            System.out.println(\"run1\");\n        }\n    }\n    ```\n    ```\n    class Impl2 implements AutoCloseable, Runnable, Serializable {\n    \n        @Override\n        public void close() {\n            System.out.println(\"closed2\");\n        }\n    \n        @Override\n        public void run() {\n            System.out.println(\"run2\");\n        }\n    }\n    ```\n1. And we want to write a method that will execute action and close the resource, something like this:\n    ```\n    static \u003cX\u003e void runAndClose(X action) {\n        try (y) {\n            y.run();\n        } catch (Exception e) {\n            // handle\n        }\n    }\n    ```\n    * where `X` is: `AutoCloseable` and `Runnable`\n1. We may try to go through additional interface:\n    * additional interface:\n        ```\n        interface RunnableAndAutoCloseable extends AutoCloseable, Runnable { }\n        ```\n    * and the method:\n        ```\n        static void runAndClose2(RunnableAndAutoCloseable y) {\n            try (y) {\n                y.run();\n            } catch (Exception e) {\n                // handle\n            }\n        }\n        ```\n    * **but note that we cannot modify `Impl1` and `Impl2` classes, so it is a road to nowhere**\n1. We may use intersection type:\n    ```\n    static \u003cI extends Runnable \u0026 AutoCloseable\u003e void runAndClose(I y) {\n        try (y) {\n            y.run();\n        } catch (Exception e) {\n            // handle\n        }\n    }\n    ```\n    and tests:\n    ```\n    Runner.runAndClose(new Impl1());\n    Runner.runAndClose(new Impl2());\n    ```\n    will print:\n    ```\n    run1\n    closed1\n    run2\n    closed2\n    ```\n# serializing lambda\nNote that using type inference and intersection types you can easily make lambda serializable:\n```\nvar serializable = (Serializable \u0026 Supplier\u003cString\u003e)() -\u003e \"a\";\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmtumilowicz%2Fjava11-intersection-type","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmtumilowicz%2Fjava11-intersection-type","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmtumilowicz%2Fjava11-intersection-type/lists"}