> Well... Perl5 didn't have typecasts primarily because it didn't have > types. To the extent it _does_ have types, it has casting, too: it > just is extremely limited. > > my $s = scalar <blah>; > my $s = list <blah>; > > The above can be construed as typecasting: converting an expression > <blah> to either a scalar or list "type", but only if it isn't that > type already.
I would argue that these are not typecasts, but instead are context casts. > > > Casting and context are closely related, and both are required (I would > hope) for multimethods. If you have a multimethod: > > sub foo (int $var); > sub foo (str $var); > > and you call it like this: > > &foo($myval); > > .... the multimethod dispatcher needs to know if $myval is closer to an > int, or a str, or something that can be easily/trivially cast to an int > or str. If I make a subclass of C<str> called C<MyString>, the &foo > dispatcher should know that a MyString is a str, and call the str-based > multimethod variant: > > my MyString $mystring = MyString.new('hello'); > &foo($mystring); # knows to call foo(str), not foo(int) Yes, but this relies on compile time types, not typecasting. It was my understanding that the dispatcher would always operate on compile time types. > My hope is that there is little difference between builtin and > user-defined types/classes, such that both use the same syntax and have > the same features wrt context and casting: > > my str $s = $var; # evaluate $var in str context > my MyClass $s = $var; # evaluate $var in MyClass context > > my $s = str $var; # cast $var to str > my $s = MyClass $var; # cast $var to MyClass > > my MyClass $myclass = MyClass.new; > my str $as_string = $myclass; # cast $myclass to string > my int $as_int = $myclass; # cast $myclass to integer Ok, I think I was confused the way you phrased typecast. We are actually talking about constructors. I don't have a problem with constructing a new value from a previously existing value when possible though the use of constructors. However, that is not what I consider a typecast mechanism where you can cast any type to any other type all willy nilly. Tanton