Creating a new type, to get strong-ish type checking and restrict usage to certain operations, using struct perhaps

Moritz Maxeiner via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jul 22 01:30:31 PDT 2017


On Saturday, 22 July 2017 at 03:18:29 UTC, Cecil Ward wrote:
> I guess part of my question, which I didn't really highlight 
> well enough, is the issue of strong typing. [...]
>
> Going back to the original example of packed bcd stored in a 
> uint64_t say, first thing is that I want to ban illegal mixing 
> of arbitrary binary values in ordinary uint64_tmtypes with 
> decimal types, again no assignment, addition, comoarisons etc 
> across types at all allowed. And no friendly automagically 
> conversions [...]

All of this should be covered by wrapping in structs and 
overloading the appropriate operators for the types in question 
[1][2][3], which is why the BCDInteger struct shell I wrote has 
the "Overload operators" comment.

[1] https://dlang.org/spec/operatoroverloading.html#binary
[2] https://dlang.org/spec/operatoroverloading.html#assignment
[3] https://dlang.org/spec/operatoroverloading.html#op-assign


More information about the Digitalmars-d-learn mailing list