perlare two methods that I had to poke around a bit to figure out that I needed them, and how to define them, but are perfectly simple when defined. I started simply looking for how to overload
sayfor Vector. I'd poked around for this a bit when I realized it was a perfectly stupid idea. I mean, that might be great for saying a single Vector, but it would be terrible for something like
say "$c is the cross product of $a and $b".
Clearly, what I really wanted to do was overload
~for Vector. I don't recall now whether I found what I was looking for in the specs or the Setting. Either way, here is the simple way to do this in the Vector class definition.
I don't quite understand the why
ouris needed here. I'm guessing it has something to do with explicitly declaring the return type (
Str). Past that, the code is perfectly straightforward and elegant.
I spent some time thinking about making a new method that took a string representation of a Vector and parsed it, sort of a reverse of the
Strmethod. But it was clear it could get very tricky with more complicated vectors -- for instance, a Vector of Vectors.
Then I realized that Perl 6 had a mechanism for outputting objects in a fashion they could be eval'ed in again:
perl. Unfortunately, the default
perlfunction just returns
"Vector.new()"when called on a Vector object. According to #perl, this is intended to automatically do the right thing for cases like this sometime in the future. In the mean time, it is easily overloaded to something that works using the above code.