Hello.

We are all familiar with this:

(1..10).WHAT   # => Range()

@foo = 1..10;

@foo.WHAT     # => Array()


When you assign a range to @foo, the result is an array. Something
similar happens, for example, if you assign a scalar to @foo... The
context of the assignment causes Perl 6 to convert the value into an
array.

I was wondering if this sort of magic is limited to pre-defined types
(arrays, hashes, etc) or if it can be extended to any class that I
might create. For example, imagine hat I create a 'Vector' class to do
basic linear algebra. Imagine that it works this way:

my @vec = Vector.new(  1,2,3,4 )

@vec * 3   # => ( 3,6,9,12 )

In other words, the '*' operator is overloaded to behave like scalar x
vector multiplication in linear algebra. I was thinking that it would
be neat if instead you could do this:


my Vector @vec;

@vec = 1,2,3,4;

@vec.WHAT   #  => Vector()


In other words, the "context" of the "=" sign tells Perl 6 to convert
the array (1,2,3,4) into a Vector() object (presumably using the
.new() method). As an additional example, you could imagine a matrix
class like this:

my Matrix @mat;

@mat = [ \[ 1,2,3], \[3,4,5], \[5,6,7] ];


In my mind, what would happen here is that the array on the right-hand
side (an array of arrays) would be converted into a matrix, which is a
2D object, and you could do regular matrix operations.

Is this sort of thing possible in Perl 6?

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

Reply via email to