Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: Runtime introspection: What good is it?

by stvn (Monsignor)
on Jul 06, 2008 at 18:39 UTC ( [id://695856]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Runtime introspection: What good is it?
in thread Runtime introspection: What good is it?

Sigh. Okay, lets try and type your code, cause if we don't check your type usage at compile time then it could just randomly blow up at runtime, which is not acceptable in most cases (and in worst case could open your code to exploits).

sub what_does_it_return () return classA | classH { return rand(1) > 0.5 ? bless( [], 'classA' ) : bless( {}, 'classH' ); }
So, assuming our language allows it, we can say that your function will return a type "union", meaning a value of either a classA or a classH type. So, now, lets try and use the variable that is returned.
my classA | classH $x = what_does_it_return();
So, the $x variable must be typed the same as the return value of the function, so it is again a type union. So now lets use $x.
$x->something()
Okay, so thats fine assuming that classA and classH all respond to the same methods right? Nope, it is not. Lets look at the definition for classA::method.
package classA; sub something (classA $self) { ... }
Our $x is a type classA | classH not classA, so it does not pass the type constraint for it's own method. This can't be good.

But wait, maybe you made classA and classH both derived from the same superclass, lets call it classX. Lets re-type your function now:

sub what_does_it_return () return classX { return rand(1) > 0.5 ? bless( [], 'classA' ) : bless( {}, 'classH' ); }
Does this buy us anything? Nope, failed again, because there is no "something" method in classX, so the compiler generates an error when you try and do this:
my classX $x = what_does_it_return(); $x->something() # compile-time blowup, classX doesn't have the "someth +ing" method!

Okay, so maybe you don't care about types, does this invalidate my points? Nope, because your code still makes the assumption that classA and classH are 100% interchangeable in all cases, they they can be easily substituted for one another anywhere in your code and Just Work. This is an ideal case, and one that only works in very restricted cases and pretty much in non-trivial programs only. The moment your bring in outside code that knows nothing of the interchangeability of classA and classH you open yourself up for a lot of errors, errors that will almost certainly happen at runtime (remember you gave up compile-time type checking already).

-stvn

Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://695856]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-18 07:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found