{"id":35012210,"url":"https://github.com/forax/destructured-visitor","last_synced_at":"2025-12-27T05:00:25.481Z","repository":{"id":197368319,"uuid":"698516935","full_name":"forax/destructured-visitor","owner":"forax","description":"Destructured Visitors are a fast but type-unsafe implementation of recursive Visitor in Java","archived":false,"fork":false,"pushed_at":"2023-10-07T07:15:13.000Z","size":25,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2023-10-07T08:21:40.240Z","etag":null,"topics":["method-handle","pattern-matcher","pattern-matching","visitor-pattern"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/forax.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-09-30T06:31:15.000Z","updated_at":"2023-10-07T08:21:45.960Z","dependencies_parsed_at":null,"dependency_job_id":"e371d4cc-a2b1-45ce-bc4a-13d812a7b473","html_url":"https://github.com/forax/destructured-visitor","commit_stats":null,"previous_names":["forax/destructured-visitor"],"tags_count":0,"template":null,"template_full_name":null,"purl":"pkg:github/forax/destructured-visitor","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forax%2Fdestructured-visitor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forax%2Fdestructured-visitor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forax%2Fdestructured-visitor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forax%2Fdestructured-visitor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/forax","download_url":"https://codeload.github.com/forax/destructured-visitor/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forax%2Fdestructured-visitor/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28072675,"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-12-27T02:00:05.897Z","response_time":58,"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":["method-handle","pattern-matcher","pattern-matching","visitor-pattern"],"created_at":"2025-12-27T05:00:06.674Z","updated_at":"2025-12-27T05:00:25.470Z","avatar_url":"https://github.com/forax.png","language":"Java","readme":"# Destructured Visitor\nDestructured Visitor is a fast by type-unsafe implementation of a recursive Visitor in Java.\n\nUnlike a classical recursive visitor or the pattern-matching (switch on types), this implementation\nuses a specific inlining cache that is not shared in between different part of the tree of instances.\n\nLet suppose we have the following hierarchies\n```java\ninterface Unit {}\nrecord Marine() implements Unit {}\nrecord Sailor() implements Unit {}\nrecord Soldier() implements Unit {}\n\ninterface Carrier {}\nrecord Boat(List\u003cUnit\u003e units) implements Carrier { }\nrecord Tank(List\u003cUnit\u003e units) implements Carrier { }\n```\n\nA way to traverse those hierarchies to compute something is to use the pattern matching\n```java\nstatic int visitCarrier(Carrier carrier) {\n  return switch (carrier) {\n    case Boat(var units) -\u003e visitUnits(units);\n    case Tank(var units) -\u003e visitUnits(units);\n  };\n}\n\nstatic int visitUnits(List\u003cUnit\u003e units) {\n  return units.stream().mapToInt(PatternMatching::visitUnit).sum();\n}\n\nstatic int visitUnit(Unit unit) {\n  return switch (unit) {\n    case Marine marine -\u003e 10;\n    case Sailor sailor -\u003e 12;\n    case Soldier soldier -\u003e 14;\n  };\n}\n\npublic static void main(String[] args) {\n  var boat = new Boat(List.of(new Marine(), new Sailor(), new Soldier()));\n  System.out.println(PatternMatching.visitCarrier(boat)));  // 36\n  ...    \n```\n\nThis is how to do it using the destructured visitor\n```java\nstatic int visit(Marine marine) { return 10; }\nstatic int visit(Sailor sailor) { return 12; }\nstatic int visit(Soldier soldier) { return 14; }\n\nstatic int visit(Boat boat, @Signature({int.class, Unit.class}) MethodHandle dispatchUnit) throws Throwable {\n  return visitUnits(boat.units(), dispatchUnit);\n}\n\nstatic int visit(Tank tank, @Signature({int.class, Unit.class}) MethodHandle dispatchUnit) throws Throwable {\n  return visitUnits(tank.units(), dispatchUnit);\n}\n\nstatic int visitUnits(List\u003cUnit\u003e units, MethodHandle dispatchUnit) throws Throwable {\n  var sum = 0;\n  for(var unit: units) {\n    sum += (int) dispatchUnit.invokeExact(unit);\n  }\n  return sum;\n}\n\nstatic final DestructuredVisitor VISITOR = DestructuredVisitor.of(MethodHandles.lookup(),\n              Arrays.stream(Visitor.class.getDeclaredMethods())\n                  .filter(m -\u003e m.getName().equals(\"visit\"))\n                  .toList());\nstatic final MethodHandle DISPATCH_CARRIER = VISITOR.createDispatch(int.class, Carrier.class);\n\nstatic int accept(Carrier carrier) throws Throwable {\n  return (int) DISPATCH_CARRIER.invokeExact(carrier);\n}\n\npublic static void main(String[] args) throws Throwable {\n  var boat = new Boat(List.of(new Marine(), new Sailor(), new Soldier()));\n  System.out.println(Visitor.accept(boat)));  // 36\n  ...  \n```\n\n\n### How to build the code\n\nYou need Java 21. The code of the DestructuredVisitor is compatible with Java 8 but the tests required Java 21.\n\nTo build with maven\n```\n  mvn package\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fforax%2Fdestructured-visitor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fforax%2Fdestructured-visitor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fforax%2Fdestructured-visitor/lists"}