good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Fun with Typed Objects 1by John M. Dlugosz (Monsignor) |
on Apr 02, 2003 at 19:54 UTC ( [id://247577]=perlmeditation: print w/replies, xml ) | Need Help?? |
In learning C#, I was (among other things) annoyed at having to write
while in C++, is sufficient. But before getting very far, I realized that in Perl we have to call new (or whatever) on everything, too. Ah, but the type is still named only once: since the variable is not typed. So, if Perl 6 is going to have much stronger compile-time awareness of types, perhaps we should have (to use the familiar Perl 5 syntax) work properly. How? My first thought was to have a typed variable autovivify to a reference of the proper type. So, since bark() is called on undef, it creates one first. Assigning a value to $skip at any point before its used would suppress that behavior. We already autovivify undef's based on how they are used: $x->[$y] will create an array ref, and we don't hear any cries from users about "well, maybe we wanted it to be undefined to indicate an error!" as in $x= find_my_array(); $x->[$y]; so extending this to members should not be any different conceptually. If you want to allow undef to mean an exception, check for it! Another idea is to have a typed variable use a default initializer if one is not specified in the statement. So, would, as part of the "my" processing, call Dog->CREATE or somesuch. If you really wanted my Dog $skip= undef; you could say so. —John
Back to
Meditations
|
|