Permit non-void return types for partial methods (VS 16.8, .NET 5)
- Dominant language
- C#
- Stars
- 12.7k
- Forks
- 1.1k
- Avg merge
- 11h 1m
- Merged PRs (30d)
- 3
Description
The Source Generator feature is being [investigated to create a runtime agnostic and AOT compatible P/Invoke stub generator](https://github.com/dotnet/runtime/pull/33742). 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.
``` CSharp
// 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`](https://docs.microsoft.com/dotnet/csharp/language-reference/keywords/partial-method) 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](https://github.com/dotnet/runtime/pull/33742#issuecomment-602290467) 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.
``` CSharp
// 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
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.