{"id":50861265,"url":"https://github.com/mununki/rescript-comptime-poc","last_synced_at":"2026-06-14T21:35:32.509Z","repository":{"id":351197120,"uuid":"1209340783","full_name":"mununki/rescript-comptime-poc","owner":"mununki","description":null,"archived":false,"fork":false,"pushed_at":"2026-04-21T08:38:05.000Z","size":42,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-04-21T10:37:50.042Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/mununki.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,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-04-13T10:28:30.000Z","updated_at":"2026-04-21T08:38:09.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/mununki/rescript-comptime-poc","commit_stats":null,"previous_names":["mununki/rescript-comptime-poc"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mununki/rescript-comptime-poc","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mununki%2Frescript-comptime-poc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mununki%2Frescript-comptime-poc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mununki%2Frescript-comptime-poc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mununki%2Frescript-comptime-poc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mununki","download_url":"https://codeload.github.com/mununki/rescript-comptime-poc/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mununki%2Frescript-comptime-poc/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34339195,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-14T02:00:07.365Z","response_time":62,"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":[],"created_at":"2026-06-14T21:35:31.753Z","updated_at":"2026-06-14T21:35:32.501Z","avatar_url":"https://github.com/mununki.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# rescript-comptime-poc\n\nSmall standalone POC project for exercising the current `comptime`\nimplementation in the sibling `../rescript` checkout.\n\nThis POC is intended to run against\n`github.com/mununki/rescript` on the `poc-comptime` branch.\n\nThe public surface used here is intentionally small:\n\n- `%comptime(...)` on top-level `let` bindings\n- `type t = %comptime(...)` for generated type aliases\n- `reflect()`\n- `field.name`, `field.typ`, `field.get(value)`\n- `item.index`, `item.typ`, `item.get(value)`\n- `constructor.name`, `constructor.payload`, `constructor.unpack(value)`, `constructor.make(payload)`\n- anonymous module witnesses such as `module({type t = user})` for type-level reflection\n\nThe sample includes generic:\n\n- `makeJsonEncoder`\n- `makeJsonDecoder`\n- `makeCopy`\n- `makeAllCases`\n- `makeVariantFromRecord`\n- `makeOptionalRecord`\n- `makeRecordFromVariant`\n\nand applies them to:\n\n- records\n- tuples\n- ordinary variants\n- `list`\n- `result`\n- generated type aliases derived from existing reflected types\n\nThe source tree is split by use case:\n\n- `src/EncoderSamples.res`: `makeJsonEncoder`\n- `src/DecoderSamples.res`: `makeJsonDecoder`\n- `src/CopySamples.res`: `makeCopy`\n- `src/AllCasesSamples.res`: `makeAllCases`\n- `src/VariantFromRecordSamples.res`: `makeVariantFromRecord`\n- `src/OptionalRecordSamples.res`: `makeOptionalRecord`\n- `src/RecordFromVariantSamples.res`: `makeRecordFromVariant`\n- `src/ComptimeValues.res`: direct compile-time evaluation examples\n\nEach sample module logs its own values at top level, so you can inspect a\nsingle example directly with Node:\n\n```sh\nnode src/EncoderSamples.mjs\nnode src/DecoderSamples.mjs\nnode src/CopySamples.mjs\nnode src/AllCasesSamples.mjs\nnode src/VariantFromRecordSamples.mjs\nnode src/OptionalRecordSamples.mjs\nnode src/RecordFromVariantSamples.mjs\nnode src/ComptimeValues.mjs\n```\n\n## Assumptions\n\n- The compiler repo lives at `/Users/mununki/github/mununki/rescript`\n- That checkout is `github.com/mununki/rescript` on the `poc-comptime` branch\n- That checkout has already been rebuilt after the `comptime` changes\n\n## Local Setup\n\nFrom the compiler checkout:\n\n```sh\ncd /Users/mununki/github/mununki/rescript\nopam exec -- dune build @install\nnode scripts/copyExes.js --compiler\n```\n\nThen in this POC project:\n\n```sh\ncd /Users/mununki/github/mununki/rescript-comptime-poc\npnpm install\npnpm build\npnpm test\n```\n\nThe project uses local `link:` dependencies, so `node_modules/rescript` points\nat the sibling compiler checkout.\n\nWhen a comptime helper needs to return an actual record value, the intended\nsurface stays close to ordinary ReScript record syntax:\n\n```rescript\nlet finish = (_obj, builder) =\u003e Some(builder)\n\nlet addField = (next, field) =\u003e\n  (obj, r) =\u003e\n    switch Dict.get(obj, field.name) {\n    | Some(valueJson) =\u003e\n      switch decodeByType(field.typ, valueJson) {\n      | Some(value) =\u003e next(obj, {...r, field: value})\n      | None =\u003e None\n      }\n    | None =\u003e None\n    }\n\nlet seed = {}\nArray.reduceRight(fields, finish, addField)(obj, seed)\n```\n\nThis keeps the decoder-style \"build a record from reflected fields\" path close\nto ordinary record update syntax. The compiler still lowers it to its existing\ninternal builder representation.\n\n## Type-Level Example\n\nThe POC includes a generated type alias that turns a record into a variant\nwithout introducing a separate `Type.*` builder API:\n\n```rescript\ntype userFieldValue = %comptime(\n  {\n    let makeVariantFromFields = fields =\u003e\n      Variant({\n        constructors:\n          fields-\u003eArray.map(field =\u003e\n            Constructor({\n              name: field.name-\u003eString.capitalize,\n              payload: Single(field.typ),\n            })\n          ),\n      })\n    let makeVariantFromRecord = _witness =\u003e\n      switch reflect() {\n      | Record({fields}) =\u003e makeVariantFromFields(fields)\n      | _ =\u003e failwith(\"userFieldValue only supports records\")\n      }\n    makeVariantFromRecord(module({type t = user}))\n  }\n)\n```\n\nAdditional generated-type samples:\n\n```rescript\ntype r0 = {name: string, age: int}\ntype optionalValueR0 = %comptime({\n  let makeOptionRecord = _witness =\u003e\n    switch reflect() {\n    | Record({fields}) =\u003e\n      Record({\n        fields:\n          fields-\u003eArray.map(field =\u003e {\n            name: field.name,\n            typ: Option(field.typ),\n          }),\n      })\n    | _ =\u003e failwith(\"makeOptionRecord only supports records\")\n    }\n  makeOptionRecord(module({type t = r0}))\n})\n\ntype optionalFieldR0 = %comptime({\n  let makeOptionalFieldRecord = _witness =\u003e\n    switch reflect() {\n    | Record({fields}) =\u003e\n      Record({\n        fields:\n          fields-\u003eArray.map(field =\u003e {\n            name: field.name,\n            typ: Optional(field.typ),\n          }),\n      })\n    | _ =\u003e failwith(\"makeOptionalFieldRecord only supports records\")\n    }\n  makeOptionalFieldRecord(module({type t = r0}))\n})\n\ntype v0 =\n  | Name(string)\n  | Age(int)\n\ntype r0 = %comptime({\n  let makeRecordFromVariant = _witness =\u003e\n    switch reflect() {\n    | Variant({constructors}) =\u003e\n      Record({\n        fields:\n          constructors-\u003eArray.map(constructor =\u003e {\n            name: switch constructor.name {\n            | \"Name\" =\u003e \"name\"\n            | \"Age\" =\u003e \"age\"\n            | _ =\u003e failwith(\"unsupported constructor\")\n            },\n            typ: switch constructor.payload {\n            | Single(desc) =\u003e desc\n            | _ =\u003e failwith(\"single-payload constructors only\")\n            },\n          }),\n      })\n    | _ =\u003e failwith(\"makeRecordFromVariant only supports variants\")\n    }\n  makeRecordFromVariant(module({type t = v0}))\n})\n```\n\n## Proving Compile-Time Evaluation\n\n`three` and `greeting` are valid `%comptime(...)` examples, but their final JS is\nnot proof by itself because ordinary compiler optimizations can also\nconstant-fold simple expressions.\n\nThe direct proof is the commented example in [`src/ComptimeValues.res`](./src/ComptimeValues.res):\n\n```rescript\nlet broken: int = %comptime(failwith(\"ran during compilation\"))\n```\n\nIf you uncomment it and run:\n\n```sh\npnpm build\n```\n\nthe build fails during compilation, before JS is emitted. That demonstrates\nthat `%comptime(...)` is being evaluated by the compiler.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmununki%2Frescript-comptime-poc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmununki%2Frescript-comptime-poc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmununki%2Frescript-comptime-poc/lists"}