develooper Front page | perl.perl6.language | Postings from April 2003

RE: HELP! Type Coercions [was Re: Do we really need the dual type system?]

Thread Previous | Thread Next
From:
Paul
Date:
April 18, 2003 11:48
Subject:
RE: HELP! Type Coercions [was Re: Do we really need the dual type system?]
Message ID:
20030418184804.10566.qmail@web41214.mail.yahoo.com
> > Ok, that's consistent with what we're all accustomed to.
> > An Array is then a collection of scalars indexed by ints, and a
> > Hash is a collection of scalars indexed by hashing a string.
> > References are scalar values that can be traced back to other
> > values.
> 
> Remember, in perl6 we are trying to make Arrays and Hashes be
> able to be stored in a scalar "place" without explicitly resorting
> to references.

Key word: "explicitly".

 $x = @y; # $x now a handle on @y
 $x[0]++; # increments @y[0]

The underlying implementation is still a ref, no?

> Will we be able to tell if an Array is a singular object which
> automatically expands in list context or if it is a list object which
> automatically unites in scalar context?  It would be nice if that was
> just an implementation detail.

An array is an array. $x isn't @y, it's just a way to get to the same
"stuff". $x is a scalar, @y is an array, and $x[0] is a scalar
dereferencing itself because you asked it nicely. "@y" might be an
array in a scalar context stringifying, but I doubt you'll get such
behavior from "$x".
 
> > Objects are references to data structures which know where to find
> > their methods because they've been blessed, and those references
> > are stored in scalars. So far so good.
> 
> I doubt we will be blessing things in perl6, except to be backward
> compatible.  Objects are objects, but not necessarily references.

The syntax looks like it won't be necessary to explicitly bless a
reference into the class, but I suspect the thing being passed around
is still going to be some sort of reference...just not to anything so
transparent as was common in P5.

> > Now, Ints are integer scalar values.
> 
> You should probably say "scalar integer value".  see below

I don't see order as significant in that particular context and
example.
 
> > So is there such a think as an Int container?
> > Or is it JUST a detail to the parser about what sort of behavior to
> > expect of this particular container?
> 
> I suspect that what makes a container class a container is that
> it does stuff with the STORE and FETCH methods.

Not relevant to my point. I was asking if the Int type was explicitly a
container, or if it was merely an optimization hint givern to the
compiler regarding the scalar variable to which it was applied. I begin
to suspect the latter, and I'm very happy about it. I don't *want* Int
objects floating about if I can just use a scalar and get a perfgomance
boost by promising the compiler I won't do non-integer things with it.

> But that doesn't exclude value type from overriding those too. 
> As far as how the container type and value type interact in such
> things, well, I haven't reached that level of enlightenment yet.

I think I may be getting there. (thanks again, Dan. :)

A scalar is a container. An Int is a kind of data, descriptive and
restrictive of what set of operations that can legally be performed on
it. Putting and Int in a scalar just means the scalar has data in it,
but declaring your scalar as an Int means that you promise not to treat
that container as if it had any other data in it, ever. Now the
compiler can make assumptions, and go "zoom".

I suspect that you could say

  my Int $i = 1;
  print $i;      # all types *do* stringify, don't they?

but now you've broken your promise, so I suspect the compiler may
gripe, or the runtime may have to slow down and hack together a quick
patch solution for your breach of contract. On the otherhand, consider

  my Int $i = 1;
  print my $s = $i;

Looks hokey -- but now you're telling the program to take that integer,
store it into a temporary and *generic* scalar space, and then print
that assignment value (which I think takes its type from the asignee?)
  
So even though it's strange, I'd think it might run faster, or at least
quiet the parser's grumbling about your being a liar.

> > What's the point of
> >   my Int $i; # ?
>         ^  ^
>         |  variable/container type
>         value type
> 
> It's a scalar variable which can only hold Int values.

Really? Wow!
Seriously, did you not understand why I asked?
(Hmm...that sounded a little sharp. Sorry. :)

It's a *scalar* which can only *hold* Int *values*.
That doesn't not mean Int is a container. $i is not an Int, it's a
*scalar*, but it can only *hold* Int *values*.

In other words, it says to the parser "make me a scalar, and I vow I
will only use it for integers." That means the parse tree (?) might be
able to skip a lot of checks and initializations and conversions.
 
> Note that you cannot find an actual value whos value type is Scalar. 
> They are always Int or Str or something more specific.

Ok, I can go for that. :)

__________________________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo
http://search.yahoo.com

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