[API Proposal]: Rename IsInstanceOfType
Background and motivation
The syntax type .IsInstanceOfType (object) is awkward.
API Proposal
namespace System.Reflection
{
public static class TypeExtensions
{
public static bool HasInstance (this Type type, object? o);
}
}
API Usage
// Fancy the value
var objects = new object[] { 0, string .Empty, false };
var type = c [0X1] .GetType ();
// Getting the values out
foreach (var s in from object o in objects where type .HasInstance (o) select o)
Console.WriteLine("{0}", s);
Alternative Designs
object .IsInstanceOfType (type) reads even better that probably would not work with null
Risks
Either of the interfaces should be merely an alias for the other one; calling an alias involves a small performance penalty. I think it will be negligible and can be optimised out.
I think the ordinary protocol for renaming things should apply:
- The new interface shall begin as an alias for the old one.
- The old interface shall be deprecated.
- The old interface should end as an alias for the new one.
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.
Tagging subscribers to this area: @dotnet/area-system-reflection See info in area-owners.md if you want to be subscribed.
Issue Details
Background and motivation
The syntax type .IsInstanceOfType (object) is awkward.
API Proposal
namespace System.Reflection
{
public static class TypeExtensions
{
public static bool HasInstance (this Type type, object? o);
}
}
API Usage
// Fancy the value
var objects = new object[] { 0, string .Empty, false };
var type = c [0X1] .GetType ();
// Getting the values out
foreach (var s in from object o in objects where type .HasInstance (o) select o)
Console.WriteLine("{0}", s);
Alternative Designs
object .IsInstanceOfType (type) reads even better that probably would not work with null
Risks
Either of the interfaces should be merely an alias for the other one; calling an alias involves a small performance penalty. I think it will be negligible and can be optimised out.
I think the ordinary protocol for renaming things should apply:
- The new interface shall begin as an alias for the old one.
- The old interface shall be deprecated.
- The old interface should end as an alias for the new one.
| Author: | yecril71pl |
|---|---|
| Assignees: | - |
| Labels: |
|
| Milestone: | - |
This entire proposal doesn't sound like a good idea.
You can easily create your own extension method if you want. Instead of introducing a breaking change that could impact thousands of developers.
And adding this function to object sounds like an even worse idea. This would add this functionality to every object in c# when there is already a perfect solution (using the is syntax) in place.
This entire proposal doesn't sound like a good idea.
You can easily create your own extension method if you want. Instead of introducing a breaking change that could impact thousands of developers.
What does this change break? It adds an extension method to a class. Any change is breaking under this interpretation.
And adding this function to
objectsounds like an even worse idea. This would add this functionality to every object in c# when there is already a perfect solution (using theissyntax) in place.
Sure, if we could use the operator is with a Type-valued expression, neither method would be necessary.
What does this change break? It adds an extension method to a class. Any change is breaking under this interpretation.
I assume according to
2.) The old interface shall be deprecated.
That eventually you wanted the IsInstanceOfType function to be deprecated in favor of HasInstance which would be a breaking change upon removal.
But all this proposal does is add confusion to the developer.
For me, the name HasInstance is even less informative and more ambiguous than IsInstanceOfType
And adding an extension method that is just an alias for another function is also confusing. Which one should a programmer use? They would (correctly) assume there would be a difference between these 2.
As I said you are more than welcome to add your own extension methods to your code if you find the naming confusing.
What does this change break? It adds an extension method to a class. Any change is breaking under this interpretation.
I assume according to
2.) The old interface shall be deprecated.
That eventually you wanted the
IsInstanceOfTypefunction to be deprecated in favor ofHasInstancewhich would be a breaking change upon removal.
It shall not be removed, the alias arrow shall be reversed.
But all this proposal does is add confusion to the developer. For me, the name
HasInstanceis even less informative and more ambiguous thanIsInstanceOfTypeAnd adding an extension method that is just an alias for another function is also confusing. Which one should a programmer use? They would (correctly) assume there would be a difference between these 2.
The difference will be that one of them is deprecated.
As I said you are more than welcome to add your own extension methods to your code if you find the naming confusing.
This poll is to establish whether it is just me. I think the naming will be confusing to everybody learning this stuff. And, more importantly, to anybody reading someone else‘s code.
But all this proposal does is add confusion to the developer. For me, the name
HasInstanceis even less informative and more ambiguous thanIsInstanceOfType
Would you find the syntax type .IsTypeOf (object) easier to understand?
The proposal is somehow reasonable, however renaming things always gets a strong negative point.
I think the current naming is unnatural, but not ambiguous: there is a type and an object, the instanceof relationship can be only applied in one direction.
The proposal is somehow reasonable, however renaming things always gets a strong negative point.
I think the current naming is unnatural, but not ambiguous: there is a type and an object, the
instanceofrelationship can be only applied in one direction.
That is unfortunately not true; an typeof (int) is an instance of type Type. However, the expression typeof (int) .IsInstanceOfType (typeof (Type)) evaluates to false.
In general, I agree that Type.IsInstanceOfType(object) sounds confusing, although as @MariovanZeist and @huoyaoyuan already mentioned changing it now will be breaking change, or deprecating it with a new name could cause even more confusion. I would not expect this API will get approval and be added to the library, though leaving the issue open for now
Thank you for the suggestion. As noted, we generally don't accept new APIs that just delegate to another to provide a different name. There needs to be a lot of value in doing so, and I don't believe that exists here. Regardless, thank you for the suggestion.