> I'd certainly like a way to easily (1) treat a string as bin/oct/hex,
> and (2) stringify a number to bin/oct/hex, because those are two pretty
> common cases.  I've tried tons of things to get a more general syntax,
> and nothing is really working.  The string interpolation case is the
> most infuriating, because it feels like something that should be easier
> than it currently is:

It's going to be hard to beat sprintf( "%x", $i ) for clarity or
conciseness.

>      using properties?
>
>      my int $i is formatted('%4x') = 255;
>      my int $i is hex = 255;
>      my int $i = 255 but formatted('%4x');
>      my int $i = 255 but hex;

It seems hard to imagine that the user would ALWAYS want to print $i out as
hex.  Wouldn't
it make more sense to put the print property with the print operator instead
of with the variable that should be able to be printed many different ways?

>      named unary op?
>
>      my str $s = hex   0xff;  # 'ff'
>      my str $s = hex:2 0xff;  # 'ff'
>      my str $s = hex:4 0xff;  # '00ff'

This is just:
my str $s = sprintf( "%x", $i );

Perhaps we need new modifiers to %x to account for your adverbial 2 & 4
above, but the
sprintf way still seems pretty clear to me.  If you wanted to define a
function to put in a module
sub hex { sprintf( "%x", shift ); }
would suffice.

>      string interpolation? (where we need it most!)
>
>      "\$i is $(sprintf('%04x',$i))"
>      "\$i is $($i but formatted('%4x') )"
>      "\$i is $($i but hex)"
>      "\$i is $(hex $i)"
>      "\$i is <hex:$i>"    # a rule-like shorthand for named formats?

my str $x = "string1" ~ sprintf("%x", $i);

To me it is still easier to read and understand.

Tanton

Reply via email to