Supertypes, subtypes, and more
bearophile via Digitalmars-d
digitalmars-d at puremagic.com
Thu Jul 10 04:50:19 PDT 2014
(Sorry for the very late answer, I have had to care for animals).
Justin Whear:
> Does this do everything you're looking for?
> http://dpaste.dzfl.pl/50531f5b1356e.
It seems OK. But there is a bit too much boilerplate. So is it
possible to create something for Phobos usable like (note the
ctor of this SocialSecurityNumber should copy the attributes of
the invariant, so if the given invariant is pure, the ctor should
be pure. This invariant is pure @safe):
mixin(Subtype!("SocialSecurityNumber", q{
import std.algorithm: all;
import std.ascii: isDigit;
assert(impl_.length == 11);
assert(impl_[0 .. 3].all!isDigit);
assert(impl_[3] == '-');
assert(impl_[4 .. 6].all!isDigit);
assert(impl_[6] == '-');
assert(impl_[7 .. 11].all!isDigit);
}));
Once issue 8864 is implemented (merging this pull:
https://github.com/D-Programming-Language/dmd/pull/3680 ) array
literals too will be allowed:
SocialSecurityNumber[] numbers = ["123-45-6789", "123-45-6785"];
Bye,
bearophile
More information about the Digitalmars-d
mailing list