Replies: 5 comments 4 replies
-
|
I guess it makes sense that this should work, but it's so easy to just add |
Beta Was this translation helpful? Give feedback.
-
|
I support this change. It would make all of the following possible: using System;
public class Program
{
public static void Main()
{
object o = "hello";
bool b = false;
// Exception: Use of unassigned local variable 's'
{
Console.WriteLine((b = o is string s) && s.Length == 0);
}
// Exception: Use of unassigned local variable 's'
{
Console.WriteLine((b = o is string s) ? s.Length == 0 : false);
}
// Exception: Use of unassigned local variable 's'
{
if ((b = o is string s) && s.Length == 0) {
Console.WriteLine(true);
}
}
// Exception: Use of unassigned local variable 's'
{
if (b = o is string s) {
Console.WriteLine(s.Length == 0);
}
}
}
}In all of the above, if you remove the |
Beta Was this translation helpful? Give feedback.
-
|
Another use case TimeSpan? expired = ObjectArray?[x]?.SomeLongProperty?.Expired;
if(expired == null || expired.Value > TimeSpan.Zero)
{
neverExpiredIcon.SetActive(true);
expiredHeader.SetActive(false);
}
else
{
neverExpiredIcon.SetActive(false);
expiredHeader.SetActive(true);
expiredHeader.text = expired.Value.ToString(format);
}can be bool active = ObjectArray?[x]?.SomeLongProperty?.Expired is TimeSpan expired && expired > TimeSpan.Zero;
neverExpiredIcon.SetActive(!active);
expiredHeader.SetActive(active);
if(active && expired > TimeSpan.Zero) // if possible to capture the control flow state in variable for later used
expiredHeader.text = expired.ToString(format); |
Beta Was this translation helpful? Give feedback.
-
|
The second thing rightly should not compile, because if ((cache is (string authCode2, string redirectURL2)) is bool pending)
{
Console.WriteLine(authCode2);
}
// means
bool temp;
temp = (cache is (string authCode2, string redirectURL2));
if (temp is bool pending)
// ^^^ This condition is always true,
// because `temp` is of compile-time (hence runtime) type `bool`.
{
// If temp == false, then authCode2 has not been assigned.
Console.WriteLine(authCode2);
}Regarding the first thing, it's not clear where the boundary is... What if you do bool b;
object o;
if (b = !!(o is int i))
{
// Should i be considered definitely assigned here?
// What about b = MethodInThisAssembly(o is int i)?
// Should compiler look into the code of MethodInThisAssembly?
// Inter-procedural analysis is not done in C# compiler.
}The current rule of C# is pretty "ignorant". Consider object o = 1;
if ((o is int a) & (o is int b))
{
// Neither a nor b is considered definitely assigned here.
}The only way to satisfy definite assignment is to use only object o = 1;
// &&
if ((o is int a) && (o is int b))
{
// ...
}
// means
int a, b;
if (o is int)
{
a = (int)o;
if (o is int)
{
b = (int)o;
{
//...
// Both a and b are considered definitely assigned here.
}
}
}In contrast, object o = 1;
// &
if ((o is int a) & (o is int b))
{
// ...
}
// means
int a, b;
bool fa, fb;
if (fa = (o is int)) a = (int)o;
if (fb = (o is int)) b = (int)o;
if (fa & fb)
{
// ...
// According to C# rules, neither a nor b is considered definitely assigned here.
} |
Beta Was this translation helpful? Give feedback.
-
|
Alternative design If there could be just a keyword to check for the state of variable assignment it might be better. Expose the state of control flow statement directly bool canExpired = ObjectArray?[x]?.SomeLongProperty?.Expired is TimeSpan expired;
neverExpiredIcon.SetActive(!canExpired);
expiredHeader.SetActive(canExpired);
if(expired is not null && expired > TimeSpan.Zero) // allow checking for variable assignment with is expression
expiredHeader.text = expired.Value.ToString(format);
if(expired is set && expired > TimeSpan.Zero) // alternatively use set keyword
expiredHeader.text = expired.Value.ToString(format);
if(expired is notnull && expired > TimeSpan.Zero) // alternatively use notnull keyword
expiredHeader.text = expired.Value.ToString(format);
if(expired is scoped && expired > TimeSpan.Zero) // alternatively use scoped keyword
expiredHeader.text = expired.Value.ToString(format);
if(required expired && expired > TimeSpan.Zero) // alternatively use required keyword
expiredHeader.text = expired.Value.ToString(format); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Since the
isexpression can be use inifstatement and also it can be apply to boolean variable like thisCurrently now it error
Should it also check for assignment if the boolean is field or variable?
Beta Was this translation helpful? Give feedback.
All reactions