[challenge] Bounded types
Philippe Sigaud
philippe.sigaud at gmail.com
Mon Oct 11 18:22:06 PDT 2010
On Sun, Oct 10, 2010 at 23:24, Adam D. Ruppe <destructionator at gmail.com> wrote:
> I saw it on the Phobos list and whipped together a first try.
Cool! Thanks, Adam.
> static string runCheckCode() { return q{
> asm {
> jo overflow;
> }
> if(_payload < min) goto overflow;
> if(_payload > max) goto overflow;
>
> goto ok;
>
> overflow:
> throw new BoundedOverflowException("Overflow at "~file~":"~to!string(line)~"
> (payload: " ~ to!string(_payload) ~ ")");
> ok:
> ;
> };
> }
Could you explain what asm { jo overflow;} does?
> T opBinary(string op, string file = __FILE__, int line = __LINE__)(T rhs) {
>
> T opOpAssign(string op, string file = __FILE__, int line = __LINE__)(T rhs) {
>
> T opAssign(T rhs, string file = __FILE__, int line = __LINE__) {
>
> string toString() {
>
> alias _payload this;
Yeah, this challenge is like re-inventing typedef. I wonder if most of
this code could be lift off to a mixin of its own, that we could use
for any type that wraps another one.
> It uses a mixin to do the check, so the jo instruction works so it catches
> wrapping overflow too.
Could you explain that part?
> I'm sure it is broken with types other than int right now, but it might be a
> starting point.
Some people downthread raise a point about performance. I don't have a
D compiler right now, do you think you could compare Bounded to a
regular int, for example?
Philippe
More information about the Digitalmars-d
mailing list