'strong types' a la boost

Charles Cooper via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Mar 14 08:45:29 PDT 2015


I think I may have answered my own question. It seems std.typecon 
provides a facility for this.
http://dlang.org/phobos/std_typecons.html#.Proxy
http://dlang.org/phobos/std_typecons.html#.Typedef

Is this the 'right' way to do things? It seems that Proxy is used 
as a mixin whereas Typedef is used to create standalone types.

Thanks
Charles

On Saturday, 14 March 2015 at 14:55:06 UTC, Charles Cooper wrote:
> I was wondering what the idiomatic D way of implementing strong 
> types. Boost has something along these lines using classes:
> http://www.boost.org/doc/libs/1_37_0/boost/strong_typedef.hpp
>
> When programming in C++ I find that the compiler does not 
> necessarily generate good code with these types, and I usually 
> use C++11 enum class, e.g.
>
> enum class dollars_t : uint32_t {}
> enum class cents_t : uint32_t {}
> /* .. code e.g. for converting between cents and dollars .. */
> void do_something_with_dollars(dollars_t) {} // this will fail 
> to compile if you try to pass it cents_t or uint32_t
>
> This is obviously a gross abuse of the enum class feature.
>
> I think there is also a way of doing this (in C++) using 
> templates a la std::chrono
>
> But enough about C++. Is there an idiomatic way of doing this 
> in D, if so what is it?
>
> Thanks!
> Charles



More information about the Digitalmars-d-learn mailing list