Use camelCase parameter names in projected struct constructors#2346
Open
Sergio0694 wants to merge 5 commits intostaging/3.0from
Open
Use camelCase parameter names in projected struct constructors#2346Sergio0694 wants to merge 5 commits intostaging/3.0from
Sergio0694 wants to merge 5 commits intostaging/3.0from
Conversation
Add a to_camel_case helper (invariant ASCII lowering) and update the struct constructor codegen to use camelCase parameter names instead of underscore-prefixed field names. For example, a struct with a 'Stage' field now generates a constructor parameter named 'stage' rather than '_Stage', producing a cleaner public API surface. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
If the first character of a struct field name is not an uppercase ASCII letter (e.g. '_Foo'), prepend an underscore instead of lowering, so the generated parameter name won't collide with the field name. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Updates the cswinrt projection generator so projected struct constructors use camelCase parameter names (instead of underscore-prefixed names), improving the public C# API surface.
Changes:
- Added
to_camel_case()helper to convert PascalCase to camelCase for generated parameter names. - Updated projected struct constructor generation to use camelCase parameter names and corresponding assignments.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/cswinrt/helpers.h |
Adds to_camel_case() utility used by codegen to derive constructor parameter names. |
src/cswinrt/code_writers.h |
Switches projected struct constructor parameter naming and assignments to use to_camel_case(field.name). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Address PR review feedback: - Route camelCase parameter names through write_escaped_identifier so that field names like 'Event' or 'Class' produce '@event'/'@Class' instead of bare C# keywords. - Precompute param_name once per field in field_info instead of calling to_camel_case twice (parameter list + assignment). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add all 46 contextual keywords from the C# language reference to the keyword list used by write_escaped_identifier. This ensures generated identifiers like 'value', 'var', 'dynamic', 'async', 'await', 'record', 'required', 'get', 'set', 'init', 'when', 'where', 'yield', etc. are properly escaped with '@' when they appear as parameter names. Reference: https://learn.microsoft.com/dotnet/csharp/language-reference/keywords Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Adding contextual keywords (e.g. 'value', 'get', 'set', 'from') to the is_keyword list caused write_escaped_identifier to add '@' prefixes to WinRT method parameter names throughout the codegen, breaking the generated C# syntax. Contextual keywords are valid identifiers outside their specific syntax contexts (e.g. 'value' is only a keyword inside property setters) and do not need '@' escaping. Only reserved keywords need escaping. The is_keyword list now has an explanatory comment and a reference URL to the C# language reference. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
manodasanW
reviewed
Mar 19, 2026
| } | ||
| else | ||
| { | ||
| result.insert(result.begin(), '_'); |
Member
There was a problem hiding this comment.
is this needed, can't we just use this.?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR updates the codegen for projected struct constructors to use camelCase parameter names instead of underscore-prefixed field names, producing a cleaner public API surface.
Motivation
The current codegen for projected structs generates constructor parameters by prepending an underscore to the field name (e.g.
_Stage,_BytesSent). This is not idiomatic C# — the standard convention for parameter names is camelCase.Changes
helpers.hAdded a
to_camel_case()utility function that:_Foobecomes__Foo), to avoid collisions with the original field namecode_writers.hUpdated the struct constructor codegen to use
to_camel_case(field.name)for parameter names and assignments.Example