Tweaking Analog Literals (C++ humor)
Saturday, August 29th, 2009Jeremy Friesner brought this site about analog literals to my attention. It provides the long-needed ability to represent integer constants in C++ not as numbers (like 42) but rather as 1-D, 2-D, or 3-D shapes whose length, area, or volume correspond to the number’s quantity. So for instance:
assert( ( o-------------o |L \ | L \ | L \ | o-------------o | ! ! ! ! ! o | ! L | ! L | ! L| ! o-------------o ).volume == ( o-------------o | ! ! ! ! ! o-------------o ).area * int(I-------------I) );
That’s great! As the inventor of Arecibo ASCII, I fully support this visual double-check with our intuitions about numbers! What if aliens are trying to read our code, but don’t know about our arbitrary choices of digits and numeric base?? This could bridge that important gap!
But there’s one nagging concern I have, which is that I don’t think the 1-D numeric values are very intuitive. Look at these examples from the site:
assert( I-I == 0 ); assert( I---I == 1 ); assert( I-----I == 2 ); assert( I-------I == 3 );
I’d prefer it to more consistently depict the historic concept of zero, and be less arbitrary with the “2N+1″ formula of dashes to implement value N. So why not overload dereference and multiply, and define “II” to be the constant value zero? This way you can get:
assert(II == 0); assert(I*I == 1); assert(I**I == 2); assert(I***I == 3);
The implementation is relatively straightforward from the proposal. But I went ahead and wrote it, and it is complete enough to give errors when compiling invalid literal specifications:
int test1 (I); // compile error! int test2 (*I); // compile error! int test3 (I*); // compile error! int test4 (*I*I); // compile error! int test5 (I*I*); // compile error!
I hope this makes it more practical for people to apply analog literals to real-world situations! Source below…
