Create custom data types

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Apr 29 21:45:50 PDT 2015


On 04/29/2015 04:30 PM, Dennis Ritchie wrote:

 > On Wednesday, 29 April 2015 at 23:22:43 UTC, Ali Çehreli wrote:
 >> On 04/29/2015 04:16 PM, Ali Çehreli wrote:
 >>
 >>>     ref CustomInteger opOpAssign(string op, T2)(T2 rhs)
 >>>     {
 >>>         static if (is (T2 == CustomInteger)) {
 >>>             mixin("value_ " ~ op ~ "= rhs.value_;");
 >>>             return this;
 >>>
 >>>         } else {
 >>>             return this.opOpAssign!(op,
 >>> CustomInteger)(CustomInteger(rhs));
 >>
 >> Playing a little more, the following shorter equivalent of the last
 >> line works as well:
 >>
 >>     return opOpAssign!op(CustomInteger(rhs));
 >>
 >> Ali
 >
 > How do I disable it?

I think I don't understand the requirements here probably because I 
don't know Ada and I don't have time to even think about such a type. 
Sorry. :-/

 > I need to prohibit all operations to the type int.
 >
 > -----
 > auto b = Balance(42);
 >
 > int t = 4;
 >
 > t += b; // error in compile time

That works because of 'alias this'. I would remove that line and start 
adding more and more operator overloads as needed.

Ali



More information about the Digitalmars-d-learn mailing list