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

2017-07-22 Thread Moritz Maxeiner via Digitalmars-d-learn

On Saturday, 22 July 2017 at 06:08:59 UTC, Cecil Ward wrote:

On Saturday, 22 July 2017 at 03:18:29 UTC, Cecil Ward wrote:

[...]


I saw David Nadlinger's units package. I'd like to know how the 
strong typing works.


By wrapping in structs and overloading operators [1][2][3][4].

[1] 
https://github.com/klickverbot/phobos/blob/units/std/units.d#L727
[2] 
https://github.com/klickverbot/phobos/blob/units/std/units.d#L736
[3] 
https://github.com/klickverbot/phobos/blob/units/std/units.d#L756
[4] 
https://github.com/klickverbot/phobos/blob/units/std/units.d#L765


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

2017-07-22 Thread Moritz Maxeiner via Digitalmars-d-learn

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


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

2017-07-21 Thread Cecil Ward via Digitalmars-d-learn

On Saturday, 22 July 2017 at 03:18:29 UTC, Cecil Ward wrote:

[...]


I saw David Nadlinger's units package. I'd like to know how the 
strong typing works.


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

2017-07-21 Thread Cecil Ward via Digitalmars-d-learn

On Friday, 21 July 2017 at 18:49:21 UTC, Cecil Ward wrote:
I was think about how to create a new type that holds packed 
bcd values, of a choice of widths, that must fit into a 
uint32_t or a uint64_t (not really long multi-byte objects). I 
am not at all sure how to do it. I thought about using a 
templated struct to simply wrap a uint of a chosen width, and 
perhaps use alias this to make things nicer.


I guess part of my question, which I didn't really highlight well 
enough, is the issue of strong typing. Example: physical units 
types, such as amps and volts, implemented as say a double or 
float or real (want to template that) but disallow evil 
assignments, comparisons, addition etc of mixed types. Another 
one would be the prevention of mixing pounds and pence by 
straight addition, or straight comparisons and blocking straight 
assignment. I'm assuming in the latter case you might use a 
machine-architecture native integral type of whatever width, 
again templating wanted. These are all really old requests, I'm 
sure, but I would appreciate a start as to how to implement the 
strong type checking in D without too much pain.


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 
from packed bcd to binary on the fly either - I want to treat 
that kind of usage as a straight bug by the user, for the moment 
at least, anyway, as I don't want to encourage silent horrible 
inefficiency creeping in.


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

2017-07-21 Thread Ali Çehreli via Digitalmars-d-learn

On 07/21/2017 11:49 AM, Cecil Ward wrote:

I was think about how to create a new type that holds packed bcd values,
of a choice of widths, that must fit into a uint32_t or a uint64_t (not
really long multi-byte objects). I am not at all sure how to do it. I
thought about using a templated struct to simply wrap a uint of a chosen
width, and perhaps use alias this to make things nicer.



Andrei's checkedint may give ideas:

  https://dlang.org/phobos/std_experimental_checkedint.html

He presented it in this talk:

  http://dconf.org/2017/talks/alexandrescu.html

... which is missing the following video link:

  https://www.youtube.com/watch?v=29h6jGtZD-U

... which has a longer version:

  https://www.youtube.com/watch?v=es6U7WAlKpQ

Ali



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

2017-07-21 Thread Moritz Maxeiner via Digitalmars-d-learn

On Friday, 21 July 2017 at 18:49:21 UTC, Cecil Ward wrote:
I was think about how to create a new type that holds packed 
bcd values, of a choice of widths, that must fit into a 
uint32_t or a uint64_t (not really long multi-byte objects). I 
am not at all sure how to do it. I thought about using a 
templated struct to simply wrap a uint of a chosen width, and 
perhaps use alias this to make things nicer.


That's usually how this is done. Take a look at the new 
std.experimental.checkedint for inspiration [1].


Here's a shell to start with (fill in:
---
struct BCDInteger(ubyte bitWidth) if (bitWidth <= 128)
{
private:
enum byteWidth = // Add bit to byte width conversion (or just 
take byte width as template parameter)

ubyte[byteWidth] store;
public:
// Add constructor(s)/static factory functions
// Overload operators,
// Add conversions to other integer formats
// Add alias this for conversion to two complements format
}
---

[1] 
https://github.com/dlang/phobos/blob/v2.075.0/std/experimental/checkedint.d#L213


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

2017-07-21 Thread Cecil Ward via Digitalmars-d-learn
I was think about how to create a new type that holds packed bcd 
values, of a choice of widths, that must fit into a uint32_t or a 
uint64_t (not really long multi-byte objects). I am not at all 
sure how to do it. I thought about using a templated struct to 
simply wrap a uint of a chosen width, and perhaps use alias this 
to make things nicer.