{"id":14990766,"url":"https://github.com/fig-eater/zig-function-overloading","last_synced_at":"2025-04-12T03:24:06.025Z","repository":{"id":245365679,"uuid":"817902095","full_name":"fig-eater/zig-function-overloading","owner":"fig-eater","description":"Explicit function overloading for the Zig programming language","archived":false,"fork":false,"pushed_at":"2024-06-24T17:21:12.000Z","size":28,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-25T22:51:32.701Z","etag":null,"topics":["zig","zig-package","ziglang"],"latest_commit_sha":null,"homepage":"","language":"Zig","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fig-eater.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-20T17:17:45.000Z","updated_at":"2025-03-05T09:21:19.000Z","dependencies_parsed_at":"2024-09-24T16:04:25.697Z","dependency_job_id":"e9623e2a-ff4c-4bbf-b786-42baba026cdd","html_url":"https://github.com/fig-eater/zig-function-overloading","commit_stats":{"total_commits":14,"total_committers":1,"mean_commits":14.0,"dds":0.0,"last_synced_commit":"b3a615bd18000272ad3acaec82cc4ec8640e5f68"},"previous_names":["fig-eater/zig-function-overloading"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fig-eater%2Fzig-function-overloading","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fig-eater%2Fzig-function-overloading/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fig-eater%2Fzig-function-overloading/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fig-eater%2Fzig-function-overloading/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fig-eater","download_url":"https://codeload.github.com/fig-eater/zig-function-overloading/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248510994,"owners_count":21116326,"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","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":["zig","zig-package","ziglang"],"created_at":"2024-09-24T14:20:47.213Z","updated_at":"2025-04-12T03:24:06.000Z","avatar_url":"https://github.com/fig-eater.png","language":"Zig","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Explicit Function Overloading for Zig\n\nSimple one-file, no-dependency, explicit function overloading for zig.\nThis all runs during compile time so running overloaded functions shouldn't have\nany runtime overhead.\n\n## Install\n\n### Using Zig Package Manager\n\nRun `zig fetch --save https://github.com/fig-eater/zig-function-overloading/archive/refs/heads/main.tar.gz`\nwithin your project directory. Run this again if you ever want to update the dependency.\n\nThen add an import to the module which needs overloading to your `build.zig`\n\nThis code is an example of how you might do this:\n\n```zig\n\nconst overloading_dependency = b.dependency(\"overloading\", .{\n    .target = target,\n    .optimize = optimize,\n});\n\n// in a default project `compile_step` might be `lib` or `exe`.\n// replace the first \"overloading\" here to avoid namespace conflicts or to change the name of the import for your project.\ncompile_step.root_module.addImport(\"overloading\", overloading_dependency.module(\"overloading\"));\n\n```\n\n### Manually\n\nDownload [src/overloading.zig](./src/overloading.zig) and save in your project.\n\nImport directly using `@import(\"path/to/overloading.zig\")`\n\n*or*\n\nSee [build.zig](./build.zig) for an example of how to use this as a local\nmodule.\n\n## Usage\n\n- Import the `overloading` module or `overloading.zig` if saved locally.\n- Call `overloading.make` with a tuple of functions, `make` will return a function which when\ncalled will call a function in the tuple with corresponding argument types.\n- All functions passed in the tuple must have the same return type.\n- Functions in the tuple cannot have the same arguments as others in the tuple.\n- If a function takes no arguments, pass `{}` into the overloaded function to call it.\n- If a function takes multiple arguments pass the arguments in a tuple.\n- If a function takes void as it's only argument pass in `.{{}}` into the overloaded function to call it\n\nExample:\n```zig\nconst std = @import(\"std\");\nconst overloading = @import(\"overloading\");\n\nfn addNoArgs() u32 {\n    return 0;\n}\nfn addU32(a: u32) u32 {\n    return a;\n}\nfn addU8Slice(as: []const u8) u32 {\n    var total: u32 = 0;\n    for (as) |a| total +|= a;\n    return total;\n}\nfn addOptionalU32(a: ?u32) u32 {\n    return if (a) |a_val| return a_val else 0;\n}\nfn addPtrU32(a_ptr: *u32) u32 {\n    return a_ptr.*;\n}\nfn addU32I32(a: u32, b: i32) u32 {\n    return a + @as(u32, @intCast(b));\n}\n\nfn printNoArgs() void {\n    std.debug.print(\"no args\\n\", .{});\n}\nfn printVoid(_: void) void {\n    std.debug.print(\"void\\n\", .{});\n}\nfn printU32(a: u32) void {\n    std.debug.print(\"{d}\\n\", .{a});\n}\nfn printU8Slice(a: []const u8) void {\n    std.debug.print(\"{s}\\n\", .{a});\n}\nfn printU32U32(a: u32, b: u32) void {\n    std.debug.print(\"{d} {d}\\n\", .{ a, b });\n}\n\nconst myAdd = overloading.make(.{\n    addNoArgs,\n    addU32,\n    addU8Slice,\n    addOptionalU32,\n    addPtrU32,\n    addU32I32,\n});\n\nconst myPrint = overloading.make(.{\n    printNoArgs,\n    printVoid,\n    printU32,\n    printU8Slice,\n    printU32U32,\n});\n\npub fn main() void {\n    const optional_with_val: ?u32 = 555;\n    const optional_with_null: ?u32 = null;\n    var a: u32 = 5;\n    _ = myAdd({}); // returns 0\n    _ = myAdd(2); // returns 2\n    _ = myAdd(\"abc\"); // returns 294\n    _ = myAdd(optional_with_val); // returns 555\n    _ = myAdd(optional_with_null); // returns 0\n    _ = myAdd(\u0026a); // returns 5\n    _ = myAdd(.{ 5, 20 }); // returns 25\n\n    myPrint({}); // prints \"no args\"\n    myPrint(.{{}}); // prints \"void\"\n    myPrint(2); // prints \"2\"\n    myPrint(\"hello\"); // prints \"hello\"\n    myPrint(.{ 3, 4 }); // prints \"3 4\"\n}\n```\nSee [example.zig](src/example.zig) for another example.\n\n## License\n\nLicensed under the Unlicense see included [LICENSE](./LICENSE) file or\nhttps://unlicense.org\nAttribution to fig / fig-eater / groakgames is appreciated but not required.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffig-eater%2Fzig-function-overloading","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffig-eater%2Fzig-function-overloading","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffig-eater%2Fzig-function-overloading/lists"}