-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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] Syntactic sugar: allow "pseudo-types" for is
-operator
#12212
Comments
What happens in the case that Also, neither Speaking of use cases, when would it use useful to know that |
@HaloFour: Well, you are right, that It would be better to implement something like this to test for multiple interfaces: bool res = "hello" is IConvertible, IEnumerable; // true, as system::string can be cast to both interfaces Concerning your Concerning your last question, whether it would be useful: I have often used generic type creation or generic type pattern matching in the past, and I think that it could be rather useful. |
While we're at it, perhaps comparisons directly on the type would also be useful: void Foo<T>()
{
//new syntax proposals:
Console.WriteLine($"T is class: {T is class}");
Console.WriteLine($"T is struct: {T is struct}");
Console.WriteLine($"T is internal: {T is internal}");
Console.WriteLine($"T is public: {T is public}");
Console.WriteLine($"T is <>: {T is <>}");
Console.WriteLine($"T is abstract: {T is abstract}");
Console.WriteLine($"T is [Serializable]: {T is [Serializable]}");
Console.WriteLine($"T is [ComVisible]: {T is [ComVisible]}");
//The previous two tests could be also combined as follows:
Console.WriteLine($"T is [Serializable, ComVisible]: {T is [Serializable, ComVisible]}");
} Foo<string>();
// T is class: true
// T is struct: false
// T is internal: false
// T is public: true
// T is <>: false
// T is abstract: false
// T is [Serializable]: true
// T is [ComVisible]: true
// T is [Serializable, ComVisible]: true
Foo<int>();
// T is class: false
// T is struct: true
// T is internal: false
// T is public: true
// T is <>: false
// T is abstract: false
// T is [Serializable]: true
// T is [ComVisible]: true
// T is [Serializable, ComVisible]: true |
Though now that I think about it, the syntax |
@bondsbw: Your last proposal could be an issue for readability in the following situation: Type t = ....;
bool res = t : public ? t : abstract ? t : class : t : struct : t : [Serializable]; is identical to: bool res = (t is public) ? ((t is abstract) ? (t is class) : (t is struct)) : (t is [Serializable]); I think that using a colon ( |
Good point. I can't think of a reason to prefer the colon syntax which justifies the confusion caused by what you wrote. |
Issue moved to dotnet/csharplang #807 via ZenHub |
I would like to propose more "pseudo-types" for the
is
-operator, as I (and many other developers) often work with reflection andSystem::Type
and have wondered, whether one could optimize code readabilty in such situations.As you can probably see, I would like to see different keywords/accessors/etc. being used with the
is
-operator, instead of using the circumbendibus syntax with withSystem::Type
:This proposal has multiple advantages:
System::Type
-instances(Especially for attribute and generic checks)
One could propose the following syntax rules for the
is
-Operator:NOTE: The expression
bool test = obj is <,,>
would match any object instanceobj
, whoms type is a generic type with exactly three generic type arguments.The text was updated successfully, but these errors were encountered: