-
Notifications
You must be signed in to change notification settings - Fork 64
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
Try
assignment/initialization
#190
Comments
Why have VB shied away from this? And can you explain what you mean by "inline assignment" and "inline declarations" (for clarity)?
Then why did C# add out variables?
100% agree. It's difficult to read. In one case, the block inside the Dim result = Try cache.TryGetValue(n) Then
' Is this true or false?
End Try
Dim result = Try cache.TryGetValue(n) Else
' Is this true or false?
End Try Not that easy to see the difference. I fail to see how this solution is any better than #60 (comment) If cache.TryGetValue(n, Dim result) Then
' Do something "result".
End If |
I think Proposal #191 is a better generalisation. |
Not entirely sure why. It's just been a thing that's been conspicuously absent for 53 years. And we shouldn't hastily discard long-standing design decisions because we don't understand them. Some languages like inline assignment expressions, some languages don't. BASIC is in the don't category, along with F#. I can advance some theories:
if (NULL == variable) ... exists purely to avoid this common mistake. By putting the constant on the left you can avoid accidentally typing this:
And despite the fact that in C# one could have always written: String s;
if ((s = obj as string) == null)
{
} Most developers I talk to refuse to write their code this way because such patterns are generally frowned upon.
My point is that it's not a historical accident or a technical constraint that has kept us from doing this, it's a repeated design decision. It's also worth noting that when we first discussed Having said all of that, the VB LDM also rejected this idea. After discussing further, I think that what we're working on with nullable types could actually solve this problem with a little work. ' Function Integer.TryParse(s As String) As Integer?
Dim value = Integer.TryParse("1")
If value IsNot Nothing Then
' value is not nullable in this scope.
End If
If value Is Nothing Then Return
' value isnot nullable after this point. With the nullable reference type feature this would be a fine pattern. We can then consider whether to do an F#-style transformation of bool/out/try methods to nullable returning versions. |
Not sure this is the right place to post this but please, as a long time VB developer, I would love if you just remove the requirement to initialize the " variable before calling the function. The syntax is already there, but I still have to initialize dpiX and dpiY before I call the function. VB allows but as far as I can tell does nothing with it. This would be a non breaking change and would make the code clearer since there is no guarantee that the variable would not be changed even if the function returns false, my initialization is meaningless (and confusing). I agree all this other stuff could be done in the future or not at all (my preference given other priorities).
|
@paul1956 we have decided to address that warning scenario you mentioned specifically. |
@AnthonyDGreen Thanks I did not see that anywhere. |
Related: #175, #159 #132 #67 #60
A general solution that solves both multiple return values and the popular .NET
TryXYZ
pattern eludes me. Though it would seem that the "Try" pattern would benefit from using either nullable value types or tuples, neither approach is satisfactory.Nullables
Tuples
It turns out that
ByRef
andOut
remain the preferred solution to this problem. This leads customers to ask us for better support forOut
vars like C# has. However, VB has long shied away in general from inline assignment and inline declarations and I am loathe to break from this. I find keeping declarations and assignments at the statement level to simplify code and indeed the use of inline assignment trickery in particular is frowned upon even in curly brace languages.After some thought, I realize that the reason general data-based solutions don't address this problem is because the "Try" pattern simultaneously conveys both data-flow and control-flow. It's meant to be used in conjunction with an
If
or other control-flow statement and the assignment of a value is relegated to a side-effect of the control-flow. I propose that a special solution is needed for this pattern that recognizes the duality:Try
assignment/initialization.Single-line
Fallback
Positive consequence/early-out
The above examples reverse the inversion the "Try" pattern forces on developers by making the control-flow aspect subordinate to the data-flow aspect. This allows type-inference to work as normal and keep variable declarations and assignments at the statement level. I considered other designs made this a new kind of statement but all of them lost those benefits.
The specifics of what patterns the feature would work on (nullables, tuples, out-vars) haven't been specified. But if designed correctly this could yield a very natural transformation for an asynchronous try:
Such a feature, combined with tuples completely eliminates the need for
ByRef
andOut
arguments outside of performance-based structure copy-avoidance; a very uncommon scenario.Feedback from 2017.10.18 LDM
The text was updated successfully, but these errors were encountered: