Re: The "=" operator and context
On 3 April 2012 20:38, Moritz Lenz wrote: > which version of Rakudo are you using? (I've tried on the last > development version from git) Rakudo Star 2012.02 % perl6 --version This is perl6 version 2012.02 built on parrot 4.1.0 revision 0 >> Hmm... So you'd have to mess with the STORE method of *all* scalars >> (i.e. not just the Vector() class) ? > > No. Just those that you want to behave specially. And I never > recommended it. Ok. Cheers, Daniel. -- I'm not overweight, I'm undertall.
Re: The "=" operator and context
On 04/03/2012 08:24 PM, Daniel Carrera wrote: > On 3 April 2012 17:24, Moritz Lenz 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; but then you don't get list assignment semantics for that last line, so it'll parse as ($vector = 1), 2, 3, 4, 5; > Doesn't work for me :-( For me the last statement gives "12". which version of Rakudo are you using? (I've tried on the last development version from git) > >> (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) ? No. Just those that you want to behave specially. And I never recommended it. Cheers, Moritz
Re: The "=" operator and context
On 3 April 2012 17:24, Moritz Lenz 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.
Re: The "=" operator and context
Am 03.04.2012 17:10, schrieb Daniel Carrera: (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. It's something inbetween. The distinction between the list assignment and scalar assignment is syntactic. In the case of list assignment, @foo.STORE(1..10) is called under the hood. 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() 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). This syntax doesn't quite work yet in Rakudo (though it wouldn't be too hard to get running), but this works: use v6; 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. You can even write my @vec := Vector.new; @vec = 1, 2, 3, 4; and get the same output. (you can also override the .STORE method of a scalar, but that's a bit creepy if you ask me). Cheers, Moritz