evalEmpire / perl5i

A single module to fix as much of Perl 5 as possible in one go
http://search.cpan.org/perldoc?perl5i
Other
156 stars 42 forks source link

Add methods to read overloading #72

Open schwern opened 14 years ago

schwern commented 14 years ago

overload.pm has some functions which would be handy to have on any object.

Will return true if the object is overloaded, false otherwise. Just a thin wrapper around overloaded::Overloaded().

This should work on a class or object and go into lib/perl5i/2/Meta.pm.

Returns the method which implements $op, if its overloaded. False otherwise. A thin wrapper around overload::Method()

Throw in some common terms for special operators.

string    => ""
numeric   => 0+

So $thing->is_overloaded("string") and $thing->is_overloaded(q[""]) are the same.

This should work on a class or object and go into lib/perl5i/2/Meta.pm.

Return the string value of the thing in the absence of overloading. A thin wrapper around overload::StrVal(). Don't know what the method should be called, it's kinda difficult to express. as_string_no_overload is descriptive but a bit awkward.

This should work on a object and go into lib/perl5i/2/Meta/Instance.pm.

Returns the method $thing will respond with when used as a string. This has to check both string and numeric overloading, because the object will fall back to its numeric overloading if it's not string overloaded.

This should work on a class or object and go into lib/perl5i/2/Meta.pm.

You can take $has_string_overload from lib/perl5i/2/Meta.pm.

The name could use some work to differentiate it from $thing->is_overloaded("string"). Something which expresses it's true purpose, "will this return an overloaded value when used as a string" is subtly different from "does this have a string overload method".

schwern commented 14 years ago

Two I've found useful is is_string_overloaded() because both 0+ and "" have to be checked. Should that be its own method, or should is_overloaded() respond to things like "string" and "number"? I like the latter.

Also, it would be nice if there was a method to add an overload to a class, rather than go through the bletcherous "use overload" interface. It can also turn on fallback by default which 99% of them time is the right thing. Also it can respond to "string" and "numeric" in addition to the cryptic "" and 0+

schwern commented 14 years ago

Thinking about it, I'd rather "string" and "number" remain straight aliases for "" and 0+ to avoid it doing different things when getting or setting. So is_string_overloaded() special method it is.

schwern commented 12 years ago

I've updated the spec for this task and reduced its scope to just checking how an object is overloaded. Methods for adding overloading is more complicated and deserves its own issue.

schwern commented 12 years ago

A GCI student has claimed this, but I've not heard from them since. http://www.google-melange.com/gci/task/view/google/gci2011/7139304

schwern commented 12 years ago

@qrazhan is working on this as part of GCI