Michael Lazzaro writes:
> Let's summarize some of the string-to-num issues:
>
> my int $i = literal "0xff"; # 255
>
>
> (3) -- We want to be able to parse a string as a number using a very
> _specific_ rule; for example, if a user is expected to enter a value in
> a specific format, like octal "755" or hex "FF", we want to "know" what
> we're expecting. That implies there may be functions for each common
> format:
>
> my int $i = sci "1e33";
> my int $i = hex "00ff";
>
the default behaiviour of "literal" should be same as perl parsing of
literals. but sub literal can have additional parameters .
I think sci and hex should be just this case :
> my int $i = sci "1e33";
my int $i = literal "1e33" : "format" => "sci" ;
> my int $i = hex "00ff";
my int $i = literal "00ff" : "format" => "hex" ;
or some *perl*-like shortcuts .
also it seems that there sould be symmetry ( as was declared ) between
types and type - casting functions
so , probably "literal" should be called "num" or "int" or "uint16" ,
depending on WYW . or the casting may be let to happen in two stages
: string -> num -> specific num type ,e.g. uint16
so these examples probably should be
> my int $i = sci "1e33";
my int $i = int "1e33" : "format" => "sci" ;
> my int $i = hex "00ff";
my int $i = int "00ff" : "format" => "hex" ;
>
> (4) -- We want to be able to output numbers to strings according to
> specific formatting rules. One way to do this (aside from using
> sprintf) is to use sprintf-style formatting strings, perhaps as
> properties to tie them to individual instances:
>
> my int $i = literal "0xff" but as_string('%02x');
>
> or explicitly each time you call the stringifyier:
>
> my str $s = str($i : base => 16 );
> my str $s = str($i : format => '%02x');
>
you see here you use the same concept : type name is same as
type-casting function . I think that was declared in Apo2 (???).
also , just to be sure , is it correct base should be "base" here ???
> or explicitly by using a class method of the number to force it:
>
> my str $s = $i.as_string('%02x');
>
>
> (5) -- An earlier proposal I had is to make C<bin>, C<oct>, C<hex>,
> etc. subclasses that overloaded their string-in and string-out methods,
> such that:
>
> my sci $i = "1e5" # 10**5
> my hex $i = "ff"; # 255
> print $i; # "FF"
> print bin $i; # "11111111"
so you say something like that :
class sci is WHAT??? {
method as_string { ... }
}
but who is ancestor ?? num , int, uint16 .
I think it is more like
my $i is as_string_opts("format"=>"sci")= "1e5" # 10**5
my $i is as_string_opts("format"=>"hex") = "ff"; # 255
print $i; # "FF"
print bin $i; # "11111111"
so now you tell perl that whatever sits inside $i , if it is going to
be stringifyed by "as_string" method -- add one more parameter to this
stringification function . something like that .
>
> which is very useful for some scripts, and would give the typecasts
> shown in (3) and (4) for free. If you wanted to do the same for any
> arbitrary format, you'd have to make your own classes with similar
> string in/out rules.
>
> Comments?
>
> MikeL
>
>
>
arcadi