in reply to Re: Fun with Typed Objects 1
in thread Fun with Typed Objects 1

Faced with the alternative I'm glad Perl isn't typed much (just scalars et al). Think about your snippets above for a bit and then be glad we don't have to worry about interfaces and up/down casting. Its because of this casting that we have so much verbosity in Java and C# in the first place.

Imagine something like this:
my $skip = (Dog) Animal->new; or my Dog $skip = Animal->new;
I think you hit the nail on the head with your:
my Dog @showdogs;
After all. Java has had to use Object with its collections to keep them generic. This leaves it to the programmer to *know* what to cast to when fetching the contents. Imagine this in Perl:
foreach my $dog(Dog @showdogs) { print $dog->bark(),"\n"; }
Eeeew. However, on the flip side perhaps some kind of autotesting at debug run time to see if an object can do what you want would be nice. So when you do the following:
method bark_at ($some_other_dog, $message)
You can specify debug code to run that uses isa() and can() on the references of your choice to test that you are passing the right stuff around.

However, I've only just thought of that and I don't know how valid or useful such an approach would be. What do you think? Example:
sub method bark_at ($some_other_dog, $message) { if(DEBUG) { die "Not valid class" unless is_obj_instance("Dog", $some_other_ +dog); } }
This function returning ok if the var is either an instance of Dog or can do all of Dogs methods. However, in this case its more like an assertation. You could then also create your own $! if you wished.

I'm not convinced of the usefulness of this though.

Replies are listed 'Best First'.
Re^3: Fun with Typed Objects 1 (see isa() considered harmful)
by Aristotle (Chancellor) on Apr 02, 2003 at 23:26 UTC