{"id":41397923,"url":"https://github.com/sebastienros/comptime","last_synced_at":"2026-02-16T11:39:54.211Z","repository":{"id":329310229,"uuid":"1119048404","full_name":"sebastienros/comptime","owner":"sebastienros","description":"Comptime brings meta-programming capabilities to C#, enabling compile-time code generation and evaluation.","archived":false,"fork":false,"pushed_at":"2025-12-18T21:49:52.000Z","size":31,"stargazers_count":555,"open_issues_count":3,"forks_count":6,"subscribers_count":7,"default_branch":"main","last_synced_at":"2026-02-15T06:23:53.860Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sebastienros.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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-12-18T16:57:53.000Z","updated_at":"2026-02-06T06:48:39.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/sebastienros/comptime","commit_stats":null,"previous_names":["sebastienros/comptime"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/sebastienros/comptime","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastienros%2Fcomptime","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastienros%2Fcomptime/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastienros%2Fcomptime/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastienros%2Fcomptime/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sebastienros","download_url":"https://codeload.github.com/sebastienros/comptime/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastienros%2Fcomptime/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29506826,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-16T09:05:14.864Z","status":"ssl_error","status_checked_at":"2026-02-16T08:55:59.364Z","response_time":115,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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-01-23T13:00:32.058Z","updated_at":"2026-02-16T11:39:54.196Z","avatar_url":"https://github.com/sebastienros.png","language":"C#","funding_links":[],"categories":["Source Generators"],"sub_categories":["Metaprogramming"],"readme":"# Comptime\n\nA .NET source generator that executes methods at compile time and serializes their results to C# code. Comptime brings meta-programming capabilities to C#, enabling compile-time code generation and evaluation.\n\n## Overview\n\nComptime allows you to mark methods with the `[Comptime]` attribute to have them executed during compilation. The return values are serialized into C# source code and used at runtime, eliminating the need for runtime computation of values that can be determined at build time.\n\nThis meta-programming approach enables developers to shift expensive computations from runtime to compile time, resulting in faster application startup and execution.\n\n## Features\n\n- **Compile-time execution**: Methods marked with `[Comptime]` are executed during compilation\n- **Method parameters**: Methods can accept parameters with compile-time constant expressions\n- **C# serialization**: Results are serialized to valid C# code\n- **Supported return types**: \n  - Primitive types: `int`, `long`, `short`, `byte`, `sbyte`, `uint`, `ulong`, `ushort`, `float`, `double`, `decimal`, `bool`, `char`, `string`\n  - Collections: `IReadOnlyList\u003cT\u003e`, `IReadOnlyDictionary\u003cTKey, TValue\u003e`, `List\u003cT\u003e`, `Dictionary\u003cTKey, TValue\u003e`\n  - Note: Arrays are **not** allowed as return types because they are mutable. Use `IReadOnlyList\u003cT\u003e` instead.\n- **Supported argument types**: Any expression that doesn't contain variables, including:\n  - Literals: `42`, `\"hello\"`, `true`\n  - Collection initializers: `new List\u003cint\u003e { 1, 2, 3 }`, `new[] { \"a\", \"b\", \"c\" }`\n  - Expressions: `1 + 2`, `Math.PI * 2`\n  - Const values and enum members\n- **Interceptor-based**: Uses C# interceptors to replace method calls with pre-computed values\n\n## Usage\n\n### Basic Usage (Parameterless Methods)\n\n```csharp\nusing Comptime;\n\npublic static partial class Constants\n{\n    [Comptime]\n    public static IReadOnlyList\u003cint\u003e GetPrimeNumbers()\n    {\n        // Complex computation that runs at compile time\n        var primes = new List\u003cint\u003e();\n        for (int i = 2; i \u003c= 100; i++)\n        {\n            if (IsPrime(i))\n                primes.Add(i);\n        }\n        return primes;\n    }\n    \n    private static bool IsPrime(int n) { /* ... */ }\n}\n\n// At runtime, calling GetPrimeNumbers() returns the pre-computed list\nvar primes = Constants.GetPrimeNumbers(); // Returns [2, 3, 5, 7, 11, ...]\n```\n\n### Methods with Parameters\n\n```csharp\nusing Comptime;\n\npublic static partial class Math\n{\n    [Comptime]\n    public static long Factorial(int n)\n    {\n        if (n \u003c= 1) return 1;\n        long result = 1;\n        for (int i = 2; i \u003c= n; i++)\n            result *= i;\n        return result;\n    }\n\n    [Comptime]\n    public static int SumList(IReadOnlyList\u003cint\u003e numbers)\n    {\n        return numbers.Sum();\n    }\n}\n\n// Each unique argument combination is computed at compile time\nvar fact5 = Math.Factorial(5);   // Pre-computed: 120\nvar fact10 = Math.Factorial(10); // Pre-computed: 3628800\n\n// Collection initializers work too!\nvar sum = Math.SumList(new List\u003cint\u003e { 1, 2, 3, 4, 5 }); // Pre-computed: 15\nvar sum2 = Math.SumList(new[] { 10, 20, 30 });           // Pre-computed: 60\n```\n\n### Generic Methods\n\n```csharp\nusing Comptime;\n\npublic static partial class Utils\n{\n    [Comptime]\n    public static int CountItems\u003cT\u003e(IReadOnlyList\u003cT\u003e items)\n    {\n        return items.Count;\n    }\n\n    [Comptime]\n    public static string JoinStrings(IReadOnlyList\u003cstring\u003e strings, string separator)\n    {\n        return string.Join(separator, strings);\n    }\n}\n\nvar count = Utils.CountItems(new[] { \"a\", \"b\", \"c\" }); // Pre-computed: 3\nvar joined = Utils.JoinStrings(new[] { \"hello\", \"world\" }, \" \"); // Pre-computed: \"hello world\"\n```\n\n## Requirements\n\n- .NET 8.0 or later\n- C# 12 or later (for interceptors support)\n\n## Installation\n\n```xml\n\u003cPackageReference Include=\"Comptime\" Version=\"1.0.0\" /\u003e\n```\n\n## How It Works\n\n1. The source generator finds methods marked with `[Comptime]`\n2. It identifies all call sites and their arguments\n3. For each unique argument combination, it executes the method at compile time\n4. The return values are serialized to C# literals/expressions\n5. Interceptor methods are generated that return the pre-computed values\n6. At runtime, calls to the original methods are intercepted and return the cached values\n\n## Diagnostics\n\n| Code | Description |\n|------|-------------|\n| COMPTIME001 | Class must be partial |\n| COMPTIME002 | Method must be static |\n| COMPTIME004 | Unsupported return type |\n| COMPTIME005 | Compilation emit failed |\n| COMPTIME006 | Method execution failed |\n| COMPTIME007 | Serialization failed |\n| COMPTIME011 | Array return type not allowed (use IReadOnlyList\u003cT\u003e) |\n| COMPTIME012 | Argument must be a constant (no variables allowed) |\n\n## Limitations\n\n- Methods must be `static`\n- The containing class must be `partial`\n- Return types must be immutable (arrays are not allowed, use `IReadOnlyList\u003cT\u003e`)\n- Method arguments must be compile-time constant expressions (no variables, only literals and expressions of literals)\n- Methods cannot have side effects that depend on runtime state\n\n## License\n\nMIT License\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsebastienros%2Fcomptime","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsebastienros%2Fcomptime","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsebastienros%2Fcomptime/lists"}