runtime icon indicating copy to clipboard operation
runtime copied to clipboard

[API Proposal]: Rename IsInstanceOfType

Open yecril71pl opened this issue 4 years ago • 11 comments

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:

  1. The new interface shall begin as an alias for the old one.
  2. The old interface shall be deprecated.
  3. The old interface should end as an alias for the new one.

yecril71pl avatar Dec 18 '21 12:12 yecril71pl

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.

ghost avatar Dec 18 '21 12:12 ghost

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:

  1. The new interface shall begin as an alias for the old one.
  2. The old interface shall be deprecated.
  3. The old interface should end as an alias for the new one.
Author: yecril71pl
Assignees: -
Labels:

api-suggestion, area-System.Reflection, untriaged

Milestone: -

ghost avatar Dec 18 '21 17:12 ghost

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.

MariovanZeist avatar Dec 18 '21 21:12 MariovanZeist

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 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.

Sure, if we could use the operator is with a Type-valued expression, neither method would be necessary.

yecril71pl avatar Dec 18 '21 21:12 yecril71pl

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.

MariovanZeist avatar Dec 18 '21 21:12 MariovanZeist

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.

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 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.

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.

yecril71pl avatar Dec 18 '21 22:12 yecril71pl

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

Would you find the syntax type .IsTypeOf (object) easier to understand?

yecril71pl avatar Dec 19 '21 07:12 yecril71pl

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.

huoyaoyuan avatar Dec 19 '21 10:12 huoyaoyuan

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.

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.

yecril71pl avatar Dec 19 '21 14:12 yecril71pl

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

buyaa-n avatar Dec 21 '21 00:12 buyaa-n

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.

stephentoub avatar Mar 14 '22 16:03 stephentoub