Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: annotation, derivation

by moritz (Cardinal)
on Feb 17, 2012 at 09:09 UTC ( [id://954477]=note: print w/replies, xml ) Need Help??


in reply to annotation, derivation

The obvious approach is to create objects that hold both the value and the annotation, and to overload all interesting operations on these objects:

use 5.010; # just for say(); use strict; use warnings; { package Annotated; sub new { my $class = shift; bless \@_, $class; } sub value { $_[0][0] }; sub annotation { $_[0][1] }; use overload '+' => sub { Annotated->new( $_[0]->value + $_[1]->value, "(" . $_[0]->annotation . ' + '. $_[1]->annotation. ')' ); } } my $foo = Annotated->new(2, 'foo'); my $bar = Annotated->new(5, 'bar'); say +($foo + $bar)->value; say +($foo + $bar)->annotation; __END__ 7 (foo + bar)

That might get hairy when you want to consider operations with non-annotated values, and it's a lot of work to overload all operators (though most of that can be automated

Apart from that I could only think of a custom perl runcore that traces annotations, but I wouldn't be the one to write such a core.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 02:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found