Skip to content

Accept template arguments as a string of fully qualified type names#3

Open
FramePerfection wants to merge 1 commit into
add-template-generatorfrom
allow-template-arguments-as-string
Open

Accept template arguments as a string of fully qualified type names#3
FramePerfection wants to merge 1 commit into
add-template-generatorfrom
allow-template-arguments-as-string

Conversation

@FramePerfection

Copy link
Copy Markdown
Member

This allows the following code to generate implementations for System.Single and System.Double by transforming the string constant NUMERIC_TYPES to their best matching INamedTypeSymbols based on the generator's semantic model.

const string NUMERIC_TYPES = "System.Single System.Double";

[ChaosGenerators.TemplateGenerator("=> a > b ? a : b;", NUMERIC_TYPES)]
internal delegate T _Max<T>(T a, T b);

[ChaosGenerators.TemplateGenerator("=> a < b ? a : b;", NUMERIC_TYPES)]
internal delegate T _Min<T>(T a, T b);

This eliminates the need to write out the set of types in each template delegate that expects the same "class" (in a mathematical sense) of objects, such as the numerics in this example.

The implementation is somewhat dirty, creating an exclusive relationship between the fullyQualifiedTypeNames and types fields in TemplateGeneratorAttribute, as well as the rather speculative implementation of the attribute argument evaluation in the TemplateGenerator.

… the 'TemplateGeneratorAttribute' constructor
@FramePerfection FramePerfection added draft This is a draft pull request. Do not merge (yet). enhancement New feature or request labels Jun 1, 2024
@FramePerfection

Copy link
Copy Markdown
Member Author

This is more of a proof of concept than a serious attempt to implement this, but since the target branch is still a draft pull-request itself, might it be okay to merge this anyways? It's missing documentation, but that should probably happen on the "main" pull-request anyways, given that the exact interface hasn't even been fully shaped yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

draft This is a draft pull request. Do not merge (yet). enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant