Add methods to read overloading #72

Open
schwern opened this Issue Jan 25, 2010 · 5 comments

1 participant

@schwern

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

  • my $is_overloaded = $thing->is_overloaded()

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.

  • my $code = $thing->is_overloaded($op);

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.

  • my $string = $object->something_something_something_str_val

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.

  • my $is_overloaded = $thing->is_string_overloaded

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

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

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

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

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

@qrazhan is working on this as part of GCI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment