Skip to content

Permit non-void return types for partial methods (VS 16.8, .NET 5) #3301

Open
@AaronRobinsonMSFT

Description

@AaronRobinsonMSFT

The Source Generator feature is being investigated to create a runtime agnostic and AOT compatible P/Invoke stub generator. One of the hard requirements of Source Generators is user written code is read only and cannot be altered by any Source Generator implementation. This requirement makes it difficult to declare methods and use them, but have a Source Generator provide an implementation in the future (i.e. build time).

Possible work-arounds exist by declaring a partial class and having a convention.

// User defined
[FutureMethod("Foo", typeof(int))]
partial class GenClasses
{
}

// Generated in another TU
partial class GenClasses
{
   public static int Foo() { ... }
}

The above could be made to work, but there are issues.

  • How does documentation for Foo work?
  • What is the IDE scenario here for IntelliSense?
  • Users would probably see many red squiggles that make development annoying.

An alternative approach would be a type of forward declaration using partial methods. The below would relax the requirement to have a void return type and thus the generation of the corresponding method could occur. IntelliSense would make sense as would a location for XML documentation in the user defined code.

Note partial methods are also required to be private. The below example does adhere to that requirement, but relaxing that would also be beneficial although not strictly needed.

// User defined
partial class GenClasses
{
   static partial int FooImpl();
   public static Foo() { return FooImpl(); }
}

// Generated in another TU
partial class GenClasses
{
   static partial int FooImpl() { ... }
}

/cc @jaredpar @davidwrighton @jkotas @stephentoub

Metadata

Metadata

Assignees

Labels

Implemented Needs ECMA SpecThis feature has been implemented in C#, but still needs to be merged into the ECMA specificationProposal champion

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions