Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discussion: mandatory partial methods #753

Closed
alrz opened this issue Jul 18, 2017 · 7 comments
Closed

Discussion: mandatory partial methods #753

alrz opened this issue Jul 18, 2017 · 7 comments

Comments

@alrz
Copy link
Contributor

alrz commented Jul 18, 2017

This was discussed under source generators, but it is still useful with current code generation mechanisms,

We can relax partial method restrictions and make that a mandatory partial method.

partial class C
{
  public partial void M();
}

partial class C 
{
  public void M() { } // produce error if absent
}

Open question: How could we define a private mandatory partial method? Currently private partial is not permitted but since the default modifier is private that could lead to confusion.

(edited to use partial instead of extern to reflect the discussion below)

@HaloFour
Copy link
Contributor

@alrz

Using the extern modifier on a method is already legal. It's used to mark methods without implementations. The compiler allows it but produces a warning if there is no attributes decorating the method such as DllImportAttribute.

@alrz
Copy link
Contributor Author

alrz commented Jul 18, 2017

Calling such methods would fail at runtime. So you should either provide an implementation (as proposed here) or add the attribute. Not sure if it already has any legitimate use cases outside DllImport.

Regarding backward compat I think this is similar to dotnet/roslyn#8456, so perhaps these could be resolved similarly. (note: that issue doesn't even produce a warning).

@alrz
Copy link
Contributor Author

alrz commented Jul 18, 2017

Furthermore, with an implementation, the warning would go away. you could opt-in to make that an error.

@HaloFour
Copy link
Contributor

@alrz

Not sure if it already has any legitimate use cases outside DllImport.

Remoting. I've used this feature myself to declare client versions of server objects where the implementations are furnished at runtime, either by remoting or through my own code. For fun I've even abused extern methods with ContextBoundObject to produce a form of P/Invoke that calls stored procedures. The application of any attribute eliminates the warning, including your own.

IIRC, mscorlib also declares various methods as extern and then has their implementation provided by the CLR.

@Happypig375
Copy link
Member

Also MethodImplAttribute which allows calls to the CLR.

@alrz
Copy link
Contributor Author

alrz commented Jul 18, 2017

So I think we're left with a couple of options,

  1. Use extern and /warnaserror:626, though the error message should be adjusted to mention partial declarations. This does not exactly make it a "mandatory partial method" because it's opt-in, just that we provide another way to shut off CS0626.
  2. Use partial but require an implementation only when it does not satisfy current restrictions on optional partial methods. Downside to this approach is that partial methods are already private, but since currently we require the access modifier to be absent, private partial could mean that it's mandatory (note: this only happens when the partial method does not violate any other restrictions i.e. a private void-returning partial method without any out parameters).
  3. Use a different keyword outright. (edit: or a combination).

@HaloFour
Copy link
Contributor

🍝 I might propose the following alternatives:

  1. private + partial
    There's nothing implied by the combination of those keywords that the method requires an implementation.
  2. abstract + partial
    Could cause confusion as abstract implies virtual but partial implies private and the two are incompatible.
  3. private + abstract
    Similar to above.
  4. No syntax changes, expand allowed use of partial methods
    Currently partial methods cannot return a value nor have ref/out parameters. The C# compiler could relax that restriction while imposing the restriction that a method that does any of those things must have an implementation. However, methods that don't return values or have ref/out parameters couldn't be made mandatory.

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

No branches or pull requests

4 participants