Temporary transform type in specific block #2501
Replies: 10 comments
-
I don't know what you dislike about the syntax below and what you really love about your own suggestion but to me the following seems pretty great and solid syntax.
Some of the suggestion you made are really weird, I mean reusing |
Beta Was this translation helpful? Give feedback.
-
@eyalsk What I don't like about that syntax? everything It like you declare object in reverse order of normally 0 = int b;
b.ToString(); It also not use = to declare variable. It just add a variable and assume that variable was set What I like about my syntax object obj;
if(obj is string) {
int l0 = obj.Length; // No error, obj temporarily be string in this block
} But it cannot do so this in C#, So I try to make a syntax that imitate it It also better because we can use only one variable, don't need to make new and keep track of it. We just want to use the origin variable so it should be in the same name ps. I think object obj;
for(var s = obj as string; s != null; s= null)
return s.Length;
// assume this is shorthand of that by dropping ;;
for(var s = obj as string)
return s.Length;
// equal to
for(obj as string)
return obj.Length; |
Beta Was this translation helpful? Give feedback.
-
A swiftish way to handle this would be if (let string s = expr) ... |
Beta Was this translation helpful? Give feedback.
-
I actually don't like let, but that seem interesting alternative maybe this? object obj;
if(let string obj)
return obj.Length; |
Beta Was this translation helpful? Give feedback.
-
I guess I can agree with you on that given that you refer to the fact that you state an expression and then you declare a variable in order to have an access to it. If it was possible I'd love to have something like this:
But I think that this isn't possible and probably been discussed so maybe something like this:
but honestly, I like the
I really dislike the fact that you have to declare the variable outside to the scope of the if statement and the reason for that is in most cases you already have existing thing that you want to operate on, why have extra noise there? also, having the variable outside the scope can lead to some weird readability issues where the variable is used inside the scope of the statement but weirdly enough it's declared outside.
|
Beta Was this translation helpful? Give feedback.
-
@eyalsk I don't know what you misunderstand about kotlin but, no it not add noise. kotlin actually remove noise It work like this // This is not C# but Kotlin
int GetIntValue(object obj)
{
if(obj is string)
return obj.Length; // obj became string in this block
if(obj is int)
return obj; // obj became int in this block
if(obj is KeyValuePair<string,int>)
return obj.Value; // obj became KeyValuePair<string,int> in this block
if(obj is IEnumerble)
return obj.Count(); // obj became IEnumerble in this block
return (int)obj.GetHashCode(); // obj is just object outside if-is block
} It is what you actually want, you already have existing thing that you want to operate on. In kotlin you don't need to make a temporary variable in the scope. Why have noise there? |
Beta Was this translation helpful? Give feedback.
-
@Thaina ahh okay, I thought you need to declare an extra variable for some reason but thanks for the clarification. So yeah, this can work pretty nicely, I think.
|
Beta Was this translation helpful? Give feedback.
-
Like |
Beta Was this translation helpful? Give feedback.
-
This makes me think of nullability tracking except with types. |
Beta Was this translation helpful? Give feedback.
-
Also, if this is implemented, I think that the type of obj in the block should be the union of its current type and whatever types/interfaces were fulfilled in order to fulfill the IF statement. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
As oppose to feature Is-As from pattern matching https://github.com/dotnet/roslyn/blob/future/docs/features/patterns.md
I would like to propose syntax for temporary transform type of variable in a block
It work like
if(a is Type)
in kotlin that would make compiler see the variablea
inif
block as a type specified automatically. But since that would be a breaking change in C# so it could be another syntax that never been used beforeIf the object is not that type, or null, then it would skip the block like it testing
if(obj is string)
Also it would be possible for
ps. Another idea is reusing keyword
try
inif
ps2. Another keyword would be better?
for(obj as string) { }
const(obj as string) { }
let(obj as string) { }
or maybe we don't need keyword
Beta Was this translation helpful? Give feedback.
All reactions