"Subclassing" int in Pike code

Chris Angelico <[email protected]>
Newsgroups gmane.comp.lang.pike.user
Message-ID <CAPTjJmp+im8wqxXnxdP3japotxOZvz-wqmxz+ukAug_O152SQw@mail.gmail.com>
I want something that basically works like an int, but appears
different in sprintf("%O"). This appears to work, but seems a little
convoluted; also, have I missed anything?

//A hexadecimal integer, with its repr being 0xNNNN.
//Basic operations on it will continue to return hex integers.
class x(int num)
{
    mixed cast(string type) {if (type=="int") return num;}
    int(0..1) is_type(string type) {if (type=="int") return 1;}
    #define lhs(x) mixed `##x(mixed ... others) {return
this_program(`##x(num,@others));}
    #define lhsrhs(x) mixed `##x(mixed other) {return this_program(num
x other);} mixed ``##x(mixed other) {return this_program(other x
num);}
    lhs(!) lhs(~) lhs(<) lhs(>)
    lhsrhs(%) lhsrhs(&) lhsrhs(*) lhsrhs(+) lhsrhs(-) lhsrhs(/)
lhsrhs(<<) lhsrhs(>>) lhsrhs(^) lhsrhs(|)
    #undef lhs
    #undef lhsrhs
    string _sprintf(int type,mapping|void params) {return
sprintf(type=='O'?"0x%*x":(string)({'%','*',type}),params||([]),num);}
}

ChrisA
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.