Skip to content

14.5.4, "Using static directives": Example needed #199

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

Closed
RexJaeschke opened this issue Aug 20, 2020 · 6 comments · Fixed by #9
Closed

14.5.4, "Using static directives": Example needed #199

RexJaeschke opened this issue Aug 20, 2020 · 6 comments · Fixed by #9
Assignees
Labels
meeting: discuss This issue should be discussed at the next TC49-TG2 meeting

Comments

@RexJaeschke
Copy link
Contributor

At the very end of this subclause we have the following:

A using_static_directive only imports members and types declared directly in the given type, not members and types declared in base classes.

TODO: Example

Ambiguities between multiple using_namespace_directives and using_static_directives are discussed in §14.5.3.

We need someone to provide the example or to argue against doing so.

@jskeet jskeet transferred this issue from another repository Jan 12, 2021
@RexJaeschke RexJaeschke added the meeting: discuss This issue should be discussed at the next TC49-TG2 meeting label Mar 1, 2021
@jskeet
Copy link
Contributor

jskeet commented Mar 10, 2021

Quick example - there may be better ones...

This compiles:

using System.Text;
using static System.Text.Encoding;

public class Test
{
    static void Main()
    {
        Encoding utf8 = UTF8; // Encoding.UTF8 is imported automatically
    }
}

But this doesn't:

using System.Text;
using static System.Text.ASCIIEncoding;

public class Test
{
    static void Main()
    {
        Encoding utf8 = UTF8; // UTF8 isn't declared directly in ASCIIEncoding
    }
}

@jskeet
Copy link
Contributor

jskeet commented Mar 10, 2021

Eric-inspired note: We can't work out the motivation behind this rule. It would be nice to note it if we can dig it up. @MadsTorgersen, any help here?

@jskeet
Copy link
Contributor

jskeet commented Mar 10, 2021

(We suspect that understanding the rule may help come up with a better example, so assigned to Mads.)

@BillWagner BillWagner self-assigned this Jun 2, 2021
@jskeet jskeet removed the meeting: discuss This issue should be discussed at the next TC49-TG2 meeting label Jun 10, 2021
@BillWagner
Copy link
Member

Per Jon's comment that mentioned @MadsTorgersen

The rule in question:

"A using_static_directive only imports members and types declared directly in the given type, not members and types declared in base classes."

I suspect the reason for this rule is that without it, any using static would import the static methods declared in System.Object: Equals(Object, Object) and ReferenceEquals(Object, Object). With any deeper hierarchy, that would be confusing.

@BillWagner
Copy link
Member

Discussed in June 30th meeting: That decision is to encourage developers to use the canonical names of types being importing in a using static rather than rely on the effects of inheritance.

A downside is that it means moving a static member to a base class should be interpreted as a breaking change, because the using static directives would be incorrect.

@BillWagner
Copy link
Member

Added in c5ce449

@jskeet jskeet added the meeting: discuss This issue should be discussed at the next TC49-TG2 meeting label Jul 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
meeting: discuss This issue should be discussed at the next TC49-TG2 meeting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants