-
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
Proposal: With block with variable declaration to allow local scope and object Ref #438
Comments
Realize that equals sign ( Hence,
Perhaps
Indeed,
|
@rskar-git The LDM's syntax for introducing variables in pattern matching (suggested by @AdamSpeight2008) could also be used here:
|
@rskar-git With X =: 3
End With But I am OK with any choice as long as it achieves the goal. |
On second though: |
If someone who doesn't know much about VB. Or indeed anyone who wasn't closely reading release notes for funky new features saw: With X =: 3 They would be heading for Google to find out what it means. However, if they saw: With 3 Into X As Integer They would be able to figure it out just by reading the statement. Now; there is a known pattern for With X := 3 Would be better. That just leaves the question about the merits of introducing new punctuation that would require explanation, where a 'wordy' version wouldn't need explaining (I'm not crazy about the actual word 'Into', but I can't think of a better one) |
Agree. But as I said:
And also, I feel that pattern matching syntax is too verbose, and seems mirrored! I may also modify @rskar-git suggestion to be: Dim X = 3
End Dim So, Dim statement can have its own block and scope! This is close to inline if statement and block if statement. But I still prefer:
|
With X = 3 if we want to name it, or see original proposal for pseudo-variable #31 |
@sbsdevx |
#437 or similar idea would be great. Most important point for me these many years wanting this change to happen is to be able to avoid declaring a variable name in these cases where the scope of the thing in question is short-lived and the name adds no inherent value to the readability of the block. The "$" proposal, whether or not that is the right identifier, is definitely the idea. Further generalizing to more local scope cases (e.g., Using blocks) also sounds incredibly useful. |
In fact, I didn't use the with block since 15 years ago at least, exept for the initialization with { }. If the expressions contain only one dot, I use the object directly, otherwise I declare a variable. Merging this declaration with With and giving it a short scope is a benefit for me that will make me reuse With blocks again. |
I think |
Personally i am prototyping the allowing of contracting the following form Using myObj As New DisposableObject()
With myObj
.Value = 123
End With
End Using into Using With myObj As New DisposableObject
.Value = 123
End Using Don't know if it would be suitable for VB.net, would need to do some analysis on a couple of large repos. |
@AdamSpeight2008
I like your refactoring but what's wrong with this:
There is no need for the with keyword! There is risk to break code that have Using nested inside With, but this risk should be calculated to estimate how often this can happen. |
Because it currently possible to nest a |
Since you've asked this question a few times, I'll try and answer.
Firstly. "Why would anyone use XXXX?" is my initial response to about 1/2 the suggestions on this forum 😄 I also ask "why would anyone use 'Select Case True' ?", but I know many people like the construct. But to answer your specific question: Sub FuBar(snafu as Integer)
With snafu = 9000
Call DoFu($)
If $ Then
DoBar()
End IF
End With
End Sub The traditional : Sub FuBar(snafu as Integer)
Dim IsIt9000 = (snafu = 9000)
Call DoFu(IsIt9000)
If IsIt9000 Then
DoBar()
End IF
End Sub Which one looks better and/or more readable, is entirely subjective, but I can see arguments for both. On your observations about ambiguity:
It is VERY important that the language be consistent. Just because a particular usage is not expected, that doesn't mean the language should have |
It is. |
I don't think your argument around But your comparison with
All those points are not really important, but this one is: Dim x = 1
Dim y = 1
With x = y
console.writeline("Foo")
End With |
Breaking changes are allowed if they don't do any harm. The issue is about "breaking" ... does it cause damage ? |
I'd be really curious to see an example of actual code that uses this construct. It would be code smell to me, but, as I figured out when I wrote up the earlier comment with an attempt at a 'prototype', it doesn't look totally wrong, nor any stranger to me than 'Select Case True', so there could well be someone using it for some weird reason (that I'd love to hear about). PS: Actually, with the 'anonymous' reference to with |
Select Case, If, Do and While statements are conditional statement that you expect to use logical conditins that yield a boolean value. So, Select case True is a statement that can have two branches, and it is a practical yet a bit strabge usage. With on the other hand is a reference stayement, that was important to optimize vb compiler performance before dot net, and shorten the code before successive dots were allowed where nested with statements were needed. Logical boolean comparisons has no place here,and boolean even didn't have any members before dot net, so no legacy code so whaever can use with true/false. So, with can only use = for assignment. Any exsotic code was using it otherwise is not only rare if exists, but also written by someone who really need to learn programming from scratch. So, let's stop defending impossible cases. Wtth Using is has a meaning in English but Using with hasn't. |
I'm not sure this is the case. In my opinion, both forms are sufficiently English-like, but neither are really valid English. |
"Do this with using... " is a regular English stelement. So, you can read the syntax as if it is Using with is some bit strange and doesn't give a meaning to vb beginners from the fist look, as most vb statements do. |
It is certainly not idiomatic. You might say "Do |
I think as my form as the following, Using this Within |
I'm talking about the Dim x = 1
Dim y = 1
Dim z = 2
Select Case x = y
Case x * y = z
console.writeline("Fu")
Case x - y = z
console.writeline("Snafu")
Case x + y = z
console.writeline("Bar")
End Select Some people like this construct, I don't. But my point is, that I don't see how |
|
@VBAndCs Function TryParse_GUID(here As Int32, <Out>ByRef Output As SyntaxNode) As Boolean
Select Case True
Case TryParse_GUID_Format_N(Here, Output)
Case TryParse_GUID_Format_D(Here, Output)
Case TryParse_GUID_Format_B(Here, Output)
Case TryParse_GUID_Format_P(Here, Output)
Case TryParse_GUID_Format_X(Here, Output)
Case Else
' Failure none of the parsing above succeeded.
Output = Nothing
End Select
Return (Output IsNot Nothing)
End Function |
OK. But threr is no similarity to With, which is a ref block with no more function. Referencing True/False gives access to basic object members, which doesn't worth creating a block, and I doubt anyone ever did that for a practical reason. |
I agree with you. But In my many years of coding, I've seen many examples of code that doesn't look 'practical' to me. Even some of the examples on this forum have left me scratching my head wondering "Why would anyone do that?" But I think that it remains a bad reason to make a potentially breaking change to a compiler - just because you and I can't see a practical use for it, doesn't mean someone else hasn't used it for something that we can't think of. That said, if we had a local, anonymous reference to the
|
Please let's add something to this effect, with no requirement for a variable name. I would use this all the time. Thank you @pricerc and others. |
@craigajohnson , Thanks for your input, but I'm the wrong person to thank, it wasn't my idea! I'm just here to highlight potential problems, and make sure we get the best VB we can. |
Disagree. The cost of fixing such rare blocks doesn't overweight the benefits of the new feature.
This proposal is a replacement for #437 and they can't coexist, so it is not possible to have any anonymous reference other than the var name! So, your code will be: With v = (doc.DocumentType = DocumentType1)
editor1.Enabled = v
editor2.Enabled = not v
editor3.Visible = v
picture1.Visible = v
End With The above code is more readable, and allows defining a local scope for var |
I don't know why you seem to be trying to prove me wrong. I already said I agree with you! I was just trying to answer your question, I was not commenting on the merits of the proposal. You asked why someone would use Furthermore, I don't think your version of my example helps your case:
Might as well be:
If the only way to access the Looking at #31 again, I have to say that I think this proposal (#438) is covered by the discussion in #31 (see Proposal 2). As is #437 (in Proposal 1). Also, as discussed in #31, named and anonymous may be able to coexist if implemented correctly. If you just want a local block, then create a proposal for that. As I mentioned in #393, Some languages allow arbitrary blocks. maybe something like: Begin ' local block
Dim v = (doc.DocumentType = DocumentType1)
editor1.Enabled = v
editor2.Enabled = not v
editor3.Visible = v
picture1.Visible = v
End ' potentially breaking change, because standalone End means 'End Program' Note, that if it were up to me, we'd scrap |
Sorry, but nothing personal I am trying to prove my main point, which using With as a local domain. And I stated with details why I lost interest in With after moving to dot net. Clearly, I will never use $ nor .Me, and if I found such codes in any source I want to reuse, I will replace them. I am not a fan of using too much dots and symbols without any real added value, on the expense of making code less readable and more confusing. |
This was #393, and the team made it clear that they will not add a new scope block #393 (comment) :
So, the only possible alternative, is to modify a legacy block to do this job beside its main job. My suggestion adds a bonus of solving the anonymous reference issue, so it can achieve two proposals in one. |
Combining this #437 (comment) with this #393
We can have a new local scope like this:
The text was updated successfully, but these errors were encountered: