develooper Front page | perl.perl5.porters | Postings from May 2013

Re: Proposal: initial type annotation in SV

Thread Previous | Thread Next
From:
demerphq
Date:
May 6, 2013 10:45
Subject:
Re: Proposal: initial type annotation in SV
Message ID:
CANgJU+WkfOMc_GD2NPuE7Cja=GwKPckebb+DYnUoUL+vmuXKDw@mail.gmail.com
On 6 May 2013 12:32, Dave Mitchell <davem@iabyn.com> wrote:
> On Mon, May 06, 2013 at 12:13:28PM +0200, demerphq wrote:
>> On 6 May 2013 12:02, Dave Mitchell <davem@iabyn.com> wrote:
>> > On Sun, May 05, 2013 at 11:45:25PM +0200, Sébastien Aperghis-Tramoni wrote:
>> >> So, here is a proposal to try making this situation a bit better:
>> >> annotate the SV to record the initial (or canonical) type of the the
>> >> value, that is the type of the value that was last affected to the SV.
>> >
>> > But I don't think that necessarily solves the problem. Consider:
>> >
>> >     while (<DATA>) {
>> >         chomp;
>> >         my ($x,$y) = split / /, $_;
>> >         my $z = sqrt($x*$x + $y*$y);
>> >         ... serialise $x, $y, $z;
>> >     }
>> >     __DATA__
>> >     0.123 1.234
>> >     ...
>> >
>> > should $x, $y be serialised as strings or floats? I think the desire of
>> > the programmer would be for them to be floats, but the canonical form is
>> > string. In other words, the initial type still doesn't necessarily DWIM,
>> > and its still up to the programmer to manually ensure the output is as
>> > desired.
>>
>> Personally i think most serialization module authors would consider
>> the canonical form to be the string, not the NV.
>>
>> Consider what should happen if the data line was
>>
>> __DATA__
>> 0.123x 0.1234x
>
> Yes, but my point is that serialization modules will often not do what the
> user really intended, and using the canonical type doesn't fix that: it
> just shuffles around the cases when it does and doesn't DWIM.
>
> I think if it's important that something gets serialised in a particular
> format, then in some way the programmer needs to tell the serialiser what
> that format should be.

This suggestion goes completely against Perl hair. Perl is a
structless, schemaless language, so how would you even specify such a
thing? It would completely prevent people from saying "serialize this
structure" without knowing in advance exactly what the structure
looked like. I am actually a little surprised you even make the
suggestion.

To me the problem here is this:

$ perl -MDevel::Peek -le'$x= "1.2345"; print $x+0; Dump $x'
1.2345
SV = PVNV(0x2378040) at 0x239ec40
  REFCNT = 1
  FLAGS = (NOK,POK,pIOK,pNOK,pPOK)
  IV = 1
  NV = 1.2345
  PV = 0x238fc60 "1.2345"\0
  CUR = 6
  LEN = 8

IMO the NOK is wrong, it should be: (POK,pIOK,pNOK,pPOK), just like it
would be here:

$ perl -MDevel::Peek -le'$x= "1.2345x"; print $x+0; Dump $x'
1.2345
SV = PVNV(0x24f7040) at 0x251dc40
  REFCNT = 1
  FLAGS = (POK,pIOK,pNOK,pPOK)
  IV = 1
  NV = 1.2345
  PV = 0x250ec60 "1.2345x"\0
  CUR = 7
  LEN = 8

From this point of view, and completely setting aside back-compat
issues (which IMO are the real killer here), the problem is fixable.

Yves


--
perl -Mre=debug -e "/just|another|perl|hacker/"

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About