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

[Proposal]: Extensions as static types #8474

Open
MadsTorgersen opened this issue Oct 2, 2024 · 8 comments
Open

[Proposal]: Extensions as static types #8474

MadsTorgersen opened this issue Oct 2, 2024 · 8 comments

Comments

@MadsTorgersen
Copy link
Contributor

MadsTorgersen commented Oct 2, 2024

Extensions as static types

This has been moved to https://github.com/dotnet/csharplang/blob/main/meetings/working-groups/extensions/extensions-as-static-types.md.

@BlinD-HuNTeR
Copy link

If we can live without those aspects of the extension feature, we can ship it faster

Please ship it already in C# 13!!! Or in the first preview of C# 14, if possible 🙂

@julealgon
Copy link

If we can live without those aspects of the extension feature, we can ship it faster and with less implementation complexity and risk. This does not prevent us from adding roles later, and in the process "upgrading" extensions to also be instance types. It allows us to further stratify the work across multiple waves of effort.

As long as it doesn't compromise future designs (which it doesn't see to, considering it can be "expanded" to non-static later), I'm all for this as well.

In a sense, this is similar to "sealed by default, open as needed" best practice. The sealed is an extra constraint that can be removed later, same as static in this context.

@333fred
Copy link
Member

333fred commented Oct 2, 2024

If we can live without those aspects of the extension feature, we can ship it faster

Please ship it already in C# 13!!! Or in the first preview of C# 14, if possible 🙂

Neither of those things are likely to happen.

@HaloFour
Copy link
Contributor

HaloFour commented Oct 3, 2024

My only question might be how this would play for helper methods. Could a private extension method be in scope within the extension that could be called from other extension members? e.g.:

public extension StringExtensions for string {
    public void M() {
        this.HelperMethod();
    }

    private void HelperMethod() { }
}

or would you be expected to declare that helper method as a static method, like we do with extension methods today?

public extension StringExtensions for string {
    public void M() {
        HelperMethod(this);
    }

    private static void HelperMethod(string value) { }
}

The former would be nice...

@TahirAhmadov
Copy link

I I'm not sure why the prohibition of:

Disallow use of an extension as an instance type. Just like static classes this means that it cannot be the type of a value or variable, and cannot be a type parameter.

Necessitates the change to how this is treated. I think we can have our cake and eat it, too, in this case.

@333fred
Copy link
Member

333fred commented Oct 3, 2024

this is an instance variable. Definitionally, if the type of an instance variable cannot be an extension, then this cannot be that extension either.

@MgSam
Copy link

MgSam commented Oct 7, 2024

Regarding the disambiguation section- will it not be possible to do: E2.M("foo")? As in, if there are ambiguities today you can always fall back to calling the method on the static class manually. Why wouldn't/shouldn't that work in the new world too?

@TahirAhmadov
Copy link

this is an instance variable. Definitionally, if the type of an instance variable cannot be an extension, then this cannot be that extension either.

I'm not sure I understand the logic here. Let's imagine this is of the extension type for syntax purposes (leaving implementation/emit strategy aside for a second). Why can't we just prohibit using the extension type as a type of fields/locals/params/properties/etc. and call it a day? Would there be negative consequences of such a prohibition while keeping this "extension typed"?

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

No branches or pull requests

7 participants