dotnet / dotnet/vblang

Units of Measure

Open
#120 11 comments 4 reactions 0 assignees View on GitHub
Proposal
Dominant language
No language data
Stars
328
Forks
71
PR merge metrics
No merged PRs in 30d

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:

1) impossible conversions will still give errors
2) we could special case it if we wanted since it's a new thing
3) 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.
4) Since often conversions in both directions would be valid it might just be an ambiguity for the compiler to choose anyway.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.