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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Strong typing can be useful at runtime too. For instance, here's a semi strong typing thing implemented with objects.
package Quantity; overload '+' => add, ... ; sub new { my $proto = shift; my $quantity = shift; my $unit = shift; bless {quantity => $quantity, unit => $unit}, ref($proto)||$proto; } sub add { my $self = shift; my $target = shift; croak "Oi! You can't add $$self{unit}s to $$target{unit}s!" unless $$self{unit} eq $$target{unit}; $self->new($self->{quantity + $target->{quantity}, $self->{unit}); }
Ooh looky, useful strong typing:
$a = Quantity->new(10, 'apple'); $b = Quantity->new(20, 'orange'); ... $c = $a + $b; # dies "Oi! you can't add apples to oranges!"
Note that the above example can be (and has been) useful extended to do dimensional analysis (is this a valid calculation), derived units (metres / seconds -> m/sec). Further possible extensions include upcasting (10 oranges + 20 apples = 30 fruit) and deferred calculations (10 EUR + 10 USD = new Sum(10 EUR, 10 USD), we'll worry about completing the calculation when we know what units we want the answer in, however 10 EUR * 10 USD = die "Meaningless calculation, don't be so bloody silly!")

Strong typing does have it's uses. And not just in the 'making the compiler go quickly' sense. Replacing your dimensionless numbers with strongly typed quantities can be a really powerful tool for helping to track down calculation errors (no more pesky mile/kilometre confusion whilst doing calculations in orbital mechanics mister rocket scientist...). And that's even before you take into account the possible benefits of deferred calculation that can be easily hung on the side of such a system.


In reply to Re: Griping about Typing by pdcawley
in thread Griping about Typing by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-03-28 16:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found