Skip to content

Implement WithCompileTimeConstants for fluent method body builders#98

Draft
Copilot wants to merge 2 commits intofeature/metehod_templatefrom
copilot/finish-withcompiletimeconstants-implementation
Draft

Implement WithCompileTimeConstants for fluent method body builders#98
Copilot wants to merge 2 commits intofeature/metehod_templatefrom
copilot/finish-withcompiletimeconstants-implementation

Conversation

Copy link
Contributor

Copilot AI commented Mar 25, 2026

The WithCompileTimeConstants API had interfaces defined in Abstractions and a CompileTimeConstants slot on BodyGenerationData, but no builder implementations or extractor support.

Stage4 builders → Stage5 transitions

Added WithCompileTimeConstants<TConstants>(Func<TConstants>) to all four Stage4 builders. The factory is invoked immediately and the materialized result stored in BodyGenerationData.CompileTimeConstants. Returns the corresponding Stage5 builder:

  • DataMethodBodyBuilderStage5WithConstants<TParam1, TReturnType, TConstants>
  • DataMethodBodyBuilderStage5NoArgWithConstants<TReturnType, TConstants>
  • DataMethodBodyBuilderStage5ReturnVoidWithConstants<TParam1, TConstants>
  • DataMethodBodyBuilderStage5ReturnVoidNoArgWithConstants<TConstants>

BodyGenerationDataExtractor

Updated TryExtractFromConstantFactory and TryExtractFromRuntimeBody to pass CompileTimeConstants as the first argument when the delegate expects a parameter and constants are present. Delegates with additional method parameters beyond constants still correctly return null (can't evaluate at compile time).

Usage

Per PiExampleFluent:

Generate.MethodBody()
    .ForMethod().WithReturnType<int>().WithParameter<int>()
    .WithCompileTimeConstants(() => new
    {
        PrecomputedTargets = keys.ToDictionary(i => i, i => Compute(i))
    })
    .UseProvidedBody((constants, decimalNumber) =>
    {
        if (constants.PrecomputedTargets.TryGetValue(decimalNumber, out int r)) return r;
        return Compute(decimalNumber);
    });

Tests

23 new tests: Stage5 builder wiring, WithCompileTimeConstants data flow, BodyGenerationDataExtractor with/without constants, factory invocation timing, and constant factory priority over runtime body.

Original prompt

This section details on the original issue you should resolve

<issue_title>WithCompileTimeConstants</issue_title>
<issue_description>There is an skeleton for API WithCompileTimeConstants in the code. However, the implementation of it is unfinished. Look at PiExampleFluent inside PiExample.cs to see how it should be implemented. Finish implementing it. Make sure to use BodyGenerationData to store the constants, no reflection/looking by string shenanigans. </issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…age5 builders

- Add WithCompileTimeConstants methods to DataMethodBodyBuilderStage4,
  DataMethodBodyBuilderStage4NoArg, DataMethodBodyBuilderStage4ReturnVoid,
  and DataMethodBodyBuilderStage4ReturnVoidNoArg
- Add Stage5 builder records: DataMethodBodyBuilderStage5WithConstants,
  DataMethodBodyBuilderStage5NoArgWithConstants,
  DataMethodBodyBuilderStage5ReturnVoidWithConstants, and
  DataMethodBodyBuilderStage5ReturnVoidNoArgWithConstants
- Update BodyGenerationDataExtractor to pass CompileTimeConstants when
  invoking ReturnConstantValueFactory and RuntimeDelegateBody delegates
- Add unit tests for all Stage5 builders and WithCompileTimeConstants flow
- Add unit tests for BodyGenerationDataExtractor with constants

Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dex3r/EasySourceGenerators/sessions/c5e01801-855b-4054-bfa6-7860c743dca7
Copilot AI changed the title [WIP] Finish implementing WithCompileTimeConstants API Implement WithCompileTimeConstants for fluent method body builders Mar 25, 2026
Copilot AI requested a review from dex3r March 25, 2026 16:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants