Skip to content

Use camelCase parameter names in projected struct constructors#2346

Open
Sergio0694 wants to merge 5 commits intostaging/3.0from
dev/sergiopedri/struct-ctor-camelcase-params
Open

Use camelCase parameter names in projected struct constructors#2346
Sergio0694 wants to merge 5 commits intostaging/3.0from
dev/sergiopedri/struct-ctor-camelcase-params

Conversation

@Sergio0694
Copy link
Member

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.h

Added a to_camel_case() utility function that:

  • Lowercases the first character using invariant ASCII arithmetic (no locale dependency)
  • Falls back to prepending an underscore if the first character is not an uppercase ASCII letter (e.g. a field named _Foo becomes __Foo), to avoid collisions with the original field name

code_writers.h

Updated the struct constructor codegen to use to_camel_case(field.name) for parameter names and assignments.

Example

// Before
public HttpProgress(HttpProgressStage _Stage, ulong _BytesSent, ulong? _TotalBytesToSend, ...)
{
    Stage = _Stage;
    BytesSent = _BytesSent;
    ...
}

// After
public HttpProgress(HttpProgressStage stage, ulong bytesSent, ulong? totalBytesToSend, ...)
{
    Stage = stage;
    BytesSent = bytesSent;
    ...
}

Sergio0694 and others added 2 commits March 18, 2026 00:24
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>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sergio0694 and others added 2 commits March 18, 2026 08:37
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>
@Sergio0694 Sergio0694 enabled auto-merge (squash) March 18, 2026 16:02
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>
}
else
{
result.insert(result.begin(), '_');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this needed, can't we just use this.?

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.

3 participants