[FR] Access static methods through instance symbols #3337
-
Designclass Foo {
public static bool Bar(string x) => x == "x";
}
// [1]
// We can do:
var b = Foo.Bar("x");
// [2]
// I propose we should also be able to do:
var foo = new Foo();
var b = foo.Bar("x"); ExamplePython's RationaleBy allowing [2], the choice of making a method
The parts I have in mind are:
This proposal provides (3.) by making switching a function between either kind seamless on the side of the caller in most cases. Until the caller explicitly needs to use the function in a context with no instance pointer. But until then, the syntax is the same. This is important because otherwise making an instance method into a static function currently requires far-reaching, needless edits to switch the syntax. Even though from the callers' side, the interface may have remained the same. The internals of the function changing from requiring instance access to not requiring it should not be a breaking change. |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments
-
It is a breaking change though. Existing binaries will break now that the function is static instead of an instance method. |
Beta Was this translation helpful? Give feedback.
-
Right. That's true of any signature changes though (here removing But I'm more interested in the source side of things :T |
Beta Was this translation helpful? Give feedback.
-
Well, this is also a source breaking change, as new methods would be considered in overload resolution, potentially choosing these new methods instead of existing ones. |
Beta Was this translation helpful? Give feedback.
-
Currently methods with the same signature that differ only in their If we're talking about best-fit overload resolution, then yes. But for consumers of the static function (previously an instance method) nothing changes. For consumers of static functions... This's an API design question. If a method suddenly makes sense to be made a static function, that's what it should be. Ideally, the naming and the overall design would mean that the newly minted static function is indeed a better fit wherever the type system decides it is a better fit. |
Beta Was this translation helpful? Give feedback.
-
I think the point is rather: public class Foo
{
public void Bar(object o) { }
public static void Bar(int i) { }
}
new Foo().Bar(3); Currently this picks the instance |
Beta Was this translation helpful? Give feedback.
-
That's exactly it canton7. |
Beta Was this translation helpful? Give feedback.
-
From the source side, I would want things to break. After all, why do I now have/need an instance that I'm calling the method off of when the method is static? I would want that all to break so I could remove all those instances. |
Beta Was this translation helpful? Give feedback.
-
This makes you forget whose responsibility class Expression {
static Expression FromText ( string source ) { ... }
}
var myExpr = new Expression();
myExpr.FromText( "24 + 8" ); This just looks wrong. And it makes you think myExpr will assign some value to itself. |
Beta Was this translation helpful? Give feedback.
I think the point is rather:
Currently this picks the instance
object
overload. If static methods were also considered, this would pick the staticint
overload. Merely introducing this change could break existing code.SharpLab