Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

[Proposal]: Partial-block only accessibility (private blockonly) #6393

Closed
TactiTac0z opened this issue Aug 20, 2022 · 3 comments
Closed

[Proposal]: Partial-block only accessibility (private blockonly) #6393

TactiTac0z opened this issue Aug 20, 2022 · 3 comments

Comments

@TactiTac0z
Copy link

Summary
A new accessibility keyword for members (or types) used within partial types similar to private except it limits accessibility to the "partial-block" in which it was defined.

Consider the example:

public partial class Test 
{
  private int x;
  void ValidTestMethod()
  {
    x++;
  }
}

//Somewhere else in our file or in a different file:
partial class Test
{
  void BrokenTestMethod()
  {
    x++;
  }
}

So far this would still be allowed, but image we replaced private with a new keyword (lets call it private blockonly for now, I'll gladly take suggestions for what the best name for the keyword would be) such that our x field looks like this:

private blockonly int x;

The increment (the reference to x) in our BrokenTestMethod method would generate an accessibility compiler error because x is defined outside of the current "partial-block"
But our ValidTestMethod won't because x is defined within the same "partial-block".

The same applies to any member defined in a partial type (including types)
A class for instance would just simply be defined as follows:

partial class Test 
{
  private blockonly class Test2 {}
}

The existing behaviour of private (both in partial types and non-partial types) will of course remain the same

Our new keyword would of course be stackable with other keyword if the user so desires such as:

private blockonly readonly ref struct TestStruct {}

just to name a long example.
Although it would NOT be stackable with other accessibility keywords like internal, protected, etc.

Motivation
I personally use partial classes a lot, having a part of my type in multiple files (or perhaps multiple places within 1 file) is my preferred way to organize my code. Some developers like making "base classes" or perhaps "static helpers classes" of which I do a fair bit too, but I could really get a lot of use of this new feature even while mixing some of those techniques in.

Like for instances if I write a method that needs fields, but those fields only gets accessed within that method or within that area of my code, blockonly would help to prevent polluting the rest of my type when writing features or methods within that type.
Another Way is to append such fields with _ or even __ which is generally how I fix such a scenario, but that is a bit messy, and I believe this feature will help with this by a lot

Editor Alternatives (Perhaps?)
If anyone knows of an visual studio extension (perhaps?) that would do something similar I would be very interested in that.

No potential .Net Issues (As far as my understanding goes)
As far as my understanding goes, this should be possible even while following the .net specifications because when partial types get compiled they all get added into the same type as if they were never partial to begin with thus the behaviour of existing runtime accessibility checks will remain the same.
This is thus syntax sugar which won't be embedded anywhere once the code is compiled

Ending
This is my first time submitting a proposal, if I'm missing any information of if I have misunderstood the guidelines please let me know so I can correct it as far as possible.
Please feel free to comment what you think about this proposal, I'm interested to see what you people out there think of it

@TactiTac0z
Copy link
Author

Please excuse my cringe name, this account was made for many years ago, I rarely use GitHub

@HaloFour
Copy link
Contributor

See: #5529

@HaloFour
Copy link
Contributor

Also, proposals should be opened as discussions.

@dotnet dotnet locked and limited conversation to collaborators Aug 20, 2022
@CyrusNajmabadi CyrusNajmabadi converted this issue into discussion #6394 Aug 20, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants