Description
This seems like it could be the kind of near-domain-specific feature that would benefit VB developers. This could useful to a host of practical applications. F# really sells the feature well here so hopefully we could borrow their design mostly intact (wouldn't it be even better if we could use F# libraries as-is?).
Right now, the syntax that seems the least ambiguous is the backtick:
Obviously this is awesome for units of length and weight:
Dim feet = 12`in
Dim ounce = 1`oz
Dim pound = 16`oz
Dim gallon = 4`qt
This might also be awesome for units of currency:
Dim minPrice = 1.00`USD
And maybe even other less concrete things like flavoring primitive types:
Dim count = arr.Length`Count
For i = 0`Index to CType(count, Index)
Dim fs = "Hello, {0}"`FormatString
Things like counts vs indexes, localized vs non-localized strings, byte encodings, etc can be represented an enforced by units of measure rather than Hungarian notation (no offense to our Hungarian sisters and brothers--we love you Hungary! <3)
I don't have a particular syntax for defining a unit in mind.
One thing I've been pondering is how this feature gels with VBs loose type conversion semantics. I've decided that even with Option Strict Off
the feature isn't wasted because:
- impossible conversions will still give errors
- we could special case it if we wanted since it's a new thing
- a big benefit of the the feature isn't so much forcing you to write out conversions but ensuring that arithmetic is done correctly for the units involved. Put another way, it doesn't matter if the compiler converts inches to millimeters before adding, it matters that the conversion happen before adding. Even if it happens implicitly you're still getting a numerically correct result.
- Since often conversions in both directions would be valid it might just be an ambiguity for the compiler to choose anyway.