Allow for non-boolean singletons in if statements #1463
Replies: 7 comments
-
I think this would only lead to confusion about what exactly does In this specific case, I believe clarity is more important than brevity. Also, it's not clear to me what a "singleton" is, since an |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
If you want this for some classes you created, you can add an implicit operator for public static implicit operator bool(Foo foo)
{
return foo.IsValid; // this shows that it does not have to be a null check.
} edit: or public static bool operator true(Foo foo) => foo.IsValid;
public static bool operator false(Foo foo) => foo.IsValid; |
Beta Was this translation helpful? Give feedback.
-
@FurkanKambay I think we could |
Beta Was this translation helpful? Give feedback.
-
@Thaina well that would require us to write |
Beta Was this translation helpful? Give feedback.
-
@FurkanKambay It seem I misunderstand your sentence |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
What if we were to allow if statements to support non-Boolean arguments if the arguments were singletons (the only argument in the if statement)? The main problem with using non-Boolean arguments in if statements seems to be having an assignment statement in the if statement such that the user doesn't realize that it is an assignment statement rather than a comparison. With singleton arguments, though, this isn't a problem and it could significantly shorten many if statements. For example:
if (variable != null)
becomesif (variable)
When evaluated in an if statement, strings could be true if they have data and false if they are empty or null. Also, structures and object could be true if they contain any public data or false if they contain only the starting values (zero, false, null, etc.).
Beta Was this translation helpful? Give feedback.
All reactions