[Proposal]: [is-inference scope] #3881
-
The
I am inspired by the Dart language! Any comments are always welcome. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 8 replies
-
Someone with the following statements asked me to reopen this.
|
Beta Was this translation helpful? Give feedback.
-
I think is more readable with a new variable... if (p is Child c)
{
c.Play();
} |
Beta Was this translation helpful? Give feedback.
-
The language can't reinterpret |
Beta Was this translation helpful? Give feedback.
-
As this is not a full syntactic and semantic proposal, I'm converting it to a discussion. |
Beta Was this translation helpful? Give feedback.
-
As @HaloFour mentioned this would be a breaking change. Here is why: In example Also, examples using System;
using System.Collections.Generic;
using System.Text;
class Program
{
static void Main()
{
// 0. your issue would break existing code, such as with "new" members
A a = new B();
if (a is B)
{
// Which "Value" would it call? A or B? you proposal would break this.
Console.WriteLine($"Your proposale would break \".Value\"...");
Console.WriteLine($"Can't determine: {{ A = {a.Value} }} or {{ B = {(a as B).Value} }}");
}
// 1. multiple "is" operators on the same variable would break your issue
// and result code that should not compile, or behave differently based
// on the number of "is" operators in the condition
if (a is int && a is string && a is StringBuilder && a is List<int>)
{
a++; // int
a.Replace("a", "has"); // string
a.Append("an identity crysis"); // StringBuilder
a.Add(0); // List<int>
}
// 2. storing the condition in a variable would not be supported
bool condition = a is StringBuilder;
if (condition)
{
((object)a as StringBuilder).Append("a reaches for a bottle of Clorox");
}
}
}
public class A { public int Value => -1; }
public class B : A { public new int Value => -2; } |
Beta Was this translation helpful? Give feedback.
-
Just spitballing, but could that be mitigated by allowing class Parent
{
public void Play() {}
}
class Child : Parent
{
public new void Play() {}
}
class Program
{
static void Main()
{
Parent p = new Child();
// p is treated as a Child instance within this block
if (p is Child p)
p.Play(); // Calls Child.Play
// From here onwards, p is now treated as Parent again
p.Play(); // Calls Parent.Play
}
} |
Beta Was this translation helpful? Give feedback.
As @HaloFour mentioned this would be a breaking change. Here is why:
In example
0
below, your feature request would be unable to determine whichValue
to call. The one onA
or the one onB
?Also, examples
1
and2
while not breaking changes are reasons alone this feature should not be considered.