-
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
Allow using on non disposable and skip using when object is null #8004
Comments
Stop using operators (like |
Your first example is something you gain with blocks by definition, {
var text = "Hello";
Console.WriteLine(text);
}
Console.WriteLine(text); // Compiler error, 'text' is not available here And your second example is nothing more than just a null-check, string text = null;
if(text != null)
{
Console.WriteLine(text);
} And I didn't understand what this has to do with pattern matching and what's "not so readable" about it. |
@alrz Didn't you see that in that proposal there are That so do the same nothing more than just a null-check |
@Thaina Actually, in this case, it's a type check and also you can use any pattern that mentioned in the spec draft. I suggest you to read it once again. |
@alrz And if you see my proposal I actually use it for type check. Because if you use I don't like it that So I was go other way around, use a |
@asvishnyakov Actually I also think that maybe Then we would do any pattern matching logic for using and make it skip by set the variable for using to null |
@Thaina Your example has no relation to pattern matching. if (expr is Type v) { // code using v } is equivalent of var v = expr as Type;
if (v != null) { // code using v } Your using proposal looks like you want to define new scope in code like var func = () =>
{
var text = "Hello";
Console.WriteLine(text); //// Hello
}
func();
Console.WriteLine(text); //// Compiler error, no text available here |
@asvishnyakov Not at all Let me state it clearly. What I really want is
While the original is try to propose I want to propose |
|
@alrz Then we could let |
@asvishnyakov As long as you don't mind double type checks, it's ok. |
@asvishnyakov OK So now I would like to propose that So the syntax will be just |
@alrz If you propose any other syntax, where we can see both casting and assigning, I will support you. Because now we always use @Thaina |
@asvishnyakov That's why I go other way around I try to reuse I also want to propose that we should be able to use |
@Thaina Ok |
This would be a breaking change. Currently the block is executed regardless of the value inside the using clause being using (null) { Console.WriteLine("This line always executes."); } |
@bondsbw Well, that's why firstly I want to propose But somehow I think we should not allow I think it should work like |
IMO Kotlin got this right: https://kotlinlang.org/docs/reference/basic-syntax.html#using-type-checks-and-automatic-casts I don't know if this could be done in C# without breaking compatibility, but I thought I would throw this in. |
@walkindude That is interesting, and I suspect you are correct that it would be a breaking change for C# due to non-virtual call resolution. Pattern matching will get us close though. if (obj is string s && s.Length > 0)
return s.Length; |
@bondsbw I would support Kotlin instead. I really hate that pattern matching syntax I think implementation will just be the same. It could be compile time temporary variable I mean when we do this if (obj is string && obj.Length > 0)
return obj.Length; At compile time it could be temporarily translate to var _tmp_obj_string = obj as string;
if (_tmp_obj_string != null && _tmp_obj_string.Length > 0)
return _tmp_obj_string.Length; And it should work just fine |
Just have another idea that would make it work like kotlin maybe just this syntax object obj = "";
using(obj as string)
{
var length = obj.Length; // obj temporarily became string in this block
} This difference from normal |
@Thaina |
Thanks @stepanbenes . I forgot that because never see anyone use it. But this was impossible anyway So I need to redo proposal to #11553 and close this |
From pattern matching proposal https://github.com/dotnet/roslyn/blob/future/docs/features/patterns.md
I don't like syntax
if (expr is Type v) { // code using v }
It not so readableSo I want to propose using anything for constraint scope.
Then I want to propose syntax using?
Which will work like if. If the variable inside that using is null then it will skip all the thing inside
ps. Alternatively I would like to let we could use operator ?? after declaration and it will both add variable to scope and return true/false
I mean like this
if ((var v = expr as Type)??) { // code using v }
Maybe betterThe text was updated successfully, but these errors were encountered: