-
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
Concessional Extensions Let
, Next
, and Incr
#231
Comments
Regarding inline assignment: #190 (comment) |
@reduckted Thanks for the comments link. I understand the philosophic stance which @AnthonyDGreen has detailed there. But I want to make it perfectly clear that I am not advocating for (new to VB) operators, and I'm not pushing for there to be more inline assignments etc. in VB coding practices. What I'm specifically suggesting is that there be a set of "official" "canonical" extensions that can take the place of |
Interesting... Yes, translators... (strokes his facial hair contemplatively) |
This is definitely interesting, I can see the use case (i.e translators). However, to make these "official" translations, they would have to be used by an official translator...I don't know if there's such a thing (the Refactor tool comes close and is thoroughly impressive) but maybe there should be (I can imagine it would make all the documentation writer's lives much easier: write a code sample in language X and voila: it can be translated to other languages Y and Z automatically or at least get you 95% of the way there so you can do the rest by hand) 👍 |
@ericmutta >...they would have to be used by an official translator. Nope, they would simply have to be reliably present in current (and future) editions of VB. At that point, people who work on translators would naturally begin to make use of them. E.g., check out the stackoverflow link I put above. It made this bit of C#:
into this for the
Notice what that translator did. It used Now look at what it did for Some translators might actually introduce Private Shared Functions for something like Now if something like
Therefore no need for any .NET Framework shenanigans or makeshift helper functions. |
OTOH, is this an "official" converter?: https://roslyncodeconverter.azurewebsites.net/ |
Ah, I'm with you now. These could be added to the My thoughts on naming: Function AssignValue(...)
Function PrefixIncrement(...)
Function PostfixIncrement(...) Using "prefix increment" and "postfix increment" as the names will make it easier for people to understand what it's actually doing, because that's what the C# docs use: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/increment-operator Still not sure if that makes it clear what they do. Maybe this 🤣: Function AssignValueAndReturnAssignedValue(...)
Function IncrementValueAndReturnOriginalValue(...)
Function IncrementValueAndReturnIncrementedValue(...) One downside I can see with these extensions, is that there is no way to handle overflows. Because the operation happens in another assembly, you can't control whether overflow checks occur, which means you'd want two versions - one with overflow checks and one without. As an aside, while writing this and testing some code I had to triple-check the docs to make sure I had the "operators" behaving correctly - reminds me of this article from Eric Lippert (see point number 3): http://www.informit.com/articles/article.aspx?p=2425867
|
Can there be decrement operators too? |
Absolutely. No point having one without the other. 👍 |
I hear you! Given the move to a more modular .NET Framework that ships as a collection of NuGet packages, perhaps the first and fastest move would be create a project here on github for two assemblies: "Translate.CsToVb.dll" and "Translate.VbToCs.dll". These would contain extensions such as your proposed Give it a go, we'll rally behind you and help where we can 👍 PS: there's a very nascent discussion at #238 on mixing C# and VB in one project. If that pans out, it may eliminate/reduce the need for translators (you would literally just copy the code verbatim and stick it in a foreign language source file that would be compiled and referenced automatically in the host project). |
Another solution would be to make a PR for icsharpcode/CodeConverter (which appears to be the de facto converter) that causes it to add local methods for assign, increment and decrement, instead of using (Actually, just having a look at the code, it already appears to do this for inline assignments - https://github.com/icsharpcode/CodeConverter/blob/a81137d15ade75a2061d4b480222e9710970b873/ICSharpCode.CodeConverter/VB/NodesVisitor.cs#L51-L55)
I'm not really sure that creating a NuGet package with the extension methods would actually solve anything. The code converters are not the things that need to use the extension methods - it's the converted code that needs the extensions. |
@reduckted > Someplace somewhere you just made some Cobol coder blush with joy! 😄 As far as namings go: Whatever the respective official "committee(s) of taste" decides on, I'm sure I can cope. I of course would prefer something terse, even if a tad mnemonic, but OK. (Frankly I don't see a problem in going with As far as handling overflows is concerned - Integral types: I would say only bother with the unchecked integer versions of extensions for As far as handling overflows is concerned - Other numeric types: We need not worry. Overflows/underflows can happen, even in C#. Not aware of any unchecked float arithmetic in C#. |
@ericmutta Mixing C# and VB in one project (as seems to have emerged from #238, which is really about full VB Support to Xamarin) sounds magical, yet I wonder on how that can be made a practical reality. In particular, what sort of refactorings and enhancements will be needed in the IDE to allow for a streamlined experience in typing and debugging? .NET already supports the notion of a .netmodule (https://blogs.msdn.microsoft.com/junfeng/2005/02/12/netmodule-vs-assembly/). That is the obvious thing to leverage. A VB project can already reference another project (of any language) that is also a member of its solution. Maybe one could devise a way to indicate that a project should be compiled as a .netmodule and linked-in instead of as a DLL to be referenced? Here's how I'd imaging such a scheme might work: Let Project V represent our (primary) VB project which results in either an EXE or DLL assembly. Let Project C represent (for example) a C# project, which must result in a DLL assembly. Have projects V and C be members of the same solution (the *.sln file). In project V, have it reference project C - not its DLL! But its project (*.csproj)! Now somewhere, somehow, within the properties of project V, have some switch set to enable project C to be a netmodule. Perhaps tucked away on the Advanced Compiler Settings, there could be a list of eligible projects with checkboxes, and checked means make it a netmodule. (Perhaps to make a project "eligible", it must be a DLL project and also not make use of this netmodule scheme itself.) The "Debug" configuration compiles project C into a DLL, and project V references the DLL. (This should ease the burden on keeping the IDE capable of delivering a streamlined typing and debugging experience.) Members marked as The "Release" configuration compiles project C into a netmodule and links it into the resulting project V assembly. (This will likely bring compromises to the IDE debugging and typing - but that's already the case anyway without netmodules.) So there you have it. Use the "Debug" configuration for development/debugging/testing, and use InternalsVisibleToAttribute as needed; the IDE should work fine since things are as they are now for multi-project solutions. Then switch to the "Release" configuration and do the compile, and all the satellite "Friend Assemblies" are become linked-in netmodules into your primary VB project assembly. Of course, I have no idea on the depths of my naivety here, let alone if it's of any help to the Xamarin problem. |
Been thinking about this more. Can this be used for non-built-in types? If so, if I overload both |
I don't know what kind of voodoo magic this Threading.Interlocked.Increment/Decrement thing is... but it's AWESOME!!! I'm playing with this idea and my thoughts are basically three extension methods to handle: int value = 1; in VB... Dim value As Integer = 1 I've placed these in an "Inline" module that contains: Enum Apply with Before and After Assign(value) Assign is generic typed, while the Incr and Decr are overloaded to handle different types. I'm thinking that there doesn't really need to be a prefix / postfix version of the inline increment and decrement. In both cases you are incrementing - the only difference between the two is whether or not you will apply the increment before or after usage within the expression; thus my approach. However, the voodoo magic that is Interlocked.Increment only allows for this on Integer, UInteger, Long and ULong types. I don't believe (or at least don't understand) how this could possibly work for Byte, SByte, Char, Short, UShort, Single, Double, etc. I do a lot of C# to VB conversions (both using tools and by hand); so this idea intrigues me greatly. I'm also considering adding these into (at some point) https://github.com/DualBrain/Community.VisualBasic Thoughts? |
Help on the function It is part of https://github.com/dotnet/coreclr and is open source. There is a PR to make it handle more types. |
It seems to be a pain point for many not to have these C# (and Java and C++ and Javascript...) operators of
=
(inline assignment), ++i (pre-increment) and i++ (post-increment) operators. Obviously (to those who know modern VB), these can be emulated well enough, especially via extentions. E.g.:I'm proposing coming up with something to standardize the VB equivalents to these, so that translations between C# and VB can go more smoothly. Among the more common complaints against VB is the lack of examples versus what's available in C#, and the use of code translators as a means to cope often get tripped up on
=
and++
. See for example https://stackoverflow.com/questions/45552216/bitcoin-blockchain-parser-c-sharp-snippet-to-vb.I'm thinking that if (built-in) standard VB equivalents to
=
and++
are made, then people who make code translators will make use of them. That in turn will facilitate VB code examples for leading-edge developments.The text was updated successfully, but these errors were encountered: