Re: "Subclassing" int in Pike code
Henrik Grubbström <[email protected]>
| Newsgroups | gmane.comp.lang.pike.user |
|---|---|
| Organization | Roxen Internet Software AB |
| Message-ID | <[email protected]> |
On Wed, 17 Sep 2014, Chris Angelico wrote:
> 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));}
^^^^
This won't do what you intend (it will just recurse until you run out of
stack or into MAX_ARGS). You need to prefix the call with predef::.
> #define lhsrhs(x) mixed `##x(mixed other) {return this_program(num
> x other);} mixed ``##x(mixed other) {return this_program(other x
> num);}
Same here.
> 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);}
> }
--
Henrik Grubbström [email protected]
Roxen Internet Software AB