Re: Interfaces and Type Safety, Dynamic Typing (OOP Newbie in Perl)
- From: "Veli-Pekka Tätilä" <vtatila@xxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 18 Aug 2005 09:10:05 +0300
Tassilo v. Parseval wrote:
<snipped heavy-handedly>
> Also sprach Veli-Pekka Tätilä:
>> Is there a good way to implement an interface in Perl or some perlish
>> idiom
> write a package where each method dies on invocation.
Ah, I see-. This semed like a crude solution until you showed the way to
make multiple methods die without repeating the bulk of the code
redundantly.
Dying is at least better than doing a pure NOP. That way you know right away
that you did something wrong. It's called defensive programming, though
sometimes Perl is not as defensive as I'd like. Again PHP is about as
defensless (is that the opposite?) as can be by default.
> That is true. Mistyped method invocations are caught at runtime. They
> have to be due to Perl's super-polymorphic nature.
Oh ok. That's still a bit nasty, considering you can force strict refs, vars
and so on, and of course normal subs canot be polymorphic so there names can
be checked at runtime just fine. But could you give me an example of a real
world project or module where dynamic typing is used to achieve something
that would be very difficult to do in a strongly and statically typed
language?
I've noticed that many OOP features appear to have very littel real-world
significance first but there are special cases in which they may be
essential. Take reflection and friend classes as examples. I was
implementing a simple XML RPC server in Java for testing purposes and
noticed that the lib I was using was able to automatically register certain
methods based on their type and name. Where as in PHP you would have to
manually fill in a method dispatch map, of course the manual way was present
in the Java implementation, too. And as for friends, I was testing some
badly written awt code with J-Unit (is there a P-Unit or something for
Perl?), and dearly wished I could just say hey gimme full access when I'm
testing. In stead I had to manually hack the code changing private parts to
public in places.
> sub func (\@) {
> "I must receive an array";
> my $ary_ref = shift; # it's passed as reference
> }
Hey thanks for this simple piece of code. I was under the impression that
you had to put a separate prototype at the top as in C, rather than
specifying it only one time in the err sub signature (compare to Java).
> Run-time checks such as isa() or ref() are the
> ones most commonly found to catch type violations.
That makes sense and is probably also sufficient for most cases. after all
it is unlikely you would assign the wrong type of reference if knowing the
class hierarchy at some level.
<module internals>
> This very liberal approach may be frowned upon by the Ada language
> designers, however each has its own virtues and shortcomings.
Agreed. Getting slightly OT in this thread again: sometimes abstraction can
be a hindrance, too. I like the fact that Perl has got regexp in the
language. If I need to do them in Java, I write and test the Perl code first
if possible.
--
With kind regards Veli-Pekka Tätilä (vtatila@xxxxxxxxxxxxxxxxxxxx)
Accessibility, game music, synthesizers and programming:
http://www.student.oulu.fi/~vtatila/
.
- Follow-Ups:
- Re: Interfaces and Type Safety, Dynamic Typing (OOP Newbie in Perl)
- From: Tassilo v. Parseval
- Re: Interfaces and Type Safety, Dynamic Typing (OOP Newbie in Perl)
- References:
- Implementing Interfaces and Type Safety (OOP Newbie in Perl)
- From: Veli-Pekka Tätilä
- Re: Implementing Interfaces and Type Safety (OOP Newbie in Perl)
- From: Tassilo v. Parseval
- Implementing Interfaces and Type Safety (OOP Newbie in Perl)
- Prev by Date: Organizing data for readability and efficiency
- Next by Date: Re: String Parsing
- Previous by thread: Re: Implementing Interfaces and Type Safety (OOP Newbie in Perl)
- Next by thread: Re: Interfaces and Type Safety, Dynamic Typing (OOP Newbie in Perl)
- Index(es):
Relevant Pages
|
|