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

Re: The "=" operator and context

Thread Previous | Thread Next
From:
Daniel Carrera
Date:
April 3, 2012 11:24
Subject:
Re: The "=" operator and context
Message ID:
CAEaabNjcEMr_9BYbsh9X+ioOR-oWDPM1gtt-otfS04PC-LiY8w@mail.gmail.com
On 3 April 2012 17:24, Moritz Lenz <moritz@faui2k3.org> wrote:
> You can, very nearly. You just need to write
>
> my @vec is Vector;
>
> because you really want to change the type of the container, not just of the
> contents (my Vector @vec would be an array containing Vector objects).


Another option might be to just use scalar variables to hold vectors:

my Vector $vector;
my Vector @array_of_vectors;

$vector = 1,2,3,4,5;


> class Vector is Array {}
> multi sub infix:<*>(Vector $a, Real $b) {
>    Vector.new( $a.list X* $b );
> }
>
> my @vec := Vector.new(1, 2, 3, 4);
> say @vec.WHAT;
> say @vec * 3;
>
> Output:
>
> Vector()
> 3 6 9 12
>
> Using binding := instead of assignment replaces the array container with a
> Vector object.

Doesn't work for me :-(  For me the last statement gives "12".


> (you can also override the .STORE method of a scalar, but that's a bit
> creepy if you ask me).

Hmm...  So you'd have to mess with the STORE method of *all* scalars
(i.e. not just the Vector() class) ?

Daniel.
-- 
I'm not overweight, I'm undertall.

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