| Store | Cart

Re: [TCLCORE] TIP 472: Add Support for 0d Radix Prefix to Integer Literals

From: Jan Nijtmans <jan....@gmail.com>
Wed, 14 Jun 2017 12:43:44 +0200
2017-06-13 21:01 GMT+02:00 Donal K. Fellows:
> In any case, it's probably for the best if we rule [format] and [scan]> changes to be out of scope for this TIP (well, if we want it to go for a> vote soon) and just stuck with literal values in Tcl code. The cases> where we're planning to change things there are cases where we would> otherwise produce errors. We can boil the ocean another day.

Well, consistency is a great think, I think the TIP would lose significance
if format would be complete left out of the proposal. However, I
would like to propose a little variation, which provides even better
compatibility.

The idea is the general meaning of the "#" modifier: Using this
modifier assures that the number - when parsed back - does
not lose its interpretation. E.g.
    % expr {[format %x 20] + 1 }
    15
    % expr {[format %#x 20] + 1}
    21

In the first example, 20 is formatted in hex as "14", but that
doesn't match with the 'meaning' of the number. Prepending
"0x" assures that the number can be parsed back, without
losing the real value.

The same holds for decimal:
    % expr {[format {% 5d} 10] + 1}
    11
    % expr {[format {%05d} 10] + 1}
    9
We see that prefixing the number with zero's results in the number
interpreted as octal. So we could use the '#' specifier to prevent
that:
    % expr {[format {%#05d} 10] + 1}
    11
Even though the number was prepended with zero's, additionally
prepending it with "0d" assures that the numerical meaning
doesn't change.

So the proposal for decimal formatting becomes: '#' in combination
with '0' prepends the number with "0d" and sufficient zero's to
make up for the total length requested. When '#' is used without
'0' it has no effect. In Tcl 9.0, zero's don't force interpretation
as octal any more. Then '#' has no effect any more.

This proposal is implemented in branch "jn-0d-radix-prefix".
Would that ease bringing it to vote? In my view, I would
fully support this for 8.6.7, since the compatibility risk is
infinitely small: I don't think any existing code uses '#'
in combination with '0'.

So .. if you want to force prefixing '0d' only:
    % format %#0d 10
    0d10
    % format %#0d -10
    -0d10

Regards,
         Jan Nijtmans

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Tcl-Core mailing list
Tcl-...@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/tcl-core

Recent Messages in this Thread
Andreas Leitgeb Jun 13, 2017 05:41 pm
Peter da Silva Jun 13, 2017 06:17 pm
Donal K. Fellows Jun 13, 2017 07:01 pm
Jan Nijtmans Jun 14, 2017 10:43 am
Kevin Kenny Jun 12, 2017 04:40 pm
Messages in this thread