Re: Distinguishing between scalars and objects (Re: RFC 21 (v1) Replace C<wantarray> with a generic C<wa)

[email protected] (Jonathan Scott Duff) Fri, 4 Aug 2000 16:37:41 -0500
Newsgroups perl.perl6.language.subs
Message-ID <[email protected]>
On Fri, Aug 04, 2000 at 02:11:30PM -0700, Nathan Wiger wrote:
> First off, nice job Damian. This is the type of want() I've always
> want()ed (excuse the pun). :-)
> 
> The one thing that isn't directly addressed (or maybe it is and I missed
> it) is how to figure out whether something wants an *generic* object or
> not.
> 
> For example, right now I'm writing the localtime() RFC, and I think it
> would be really cool if want() could somehow distinguish between these:
> 
>    $date  =  localtime();     # scalar date
>    $obj   =  localtime();     # localtime obj
> 
> That way, you would be able to get the scalar $date, as well as an $obj
> that would let you get $obj->year directly.

I think we're already there.  In your example above, $date would be an
overloaded "object".  For example:

	$date = localtime();	# date is an object that knows how to:
	print scalar $date;	# output itself in scalar context
	$year = $date->{year};	# be accessed as a method

We just need a hook (think "tie") to make $date morph as needed.  Of
course, we'd probably need to expand all of the special tie methods
(FETCH, STORE, etc.)

> but doesn't seem to mention the generic case; i.e., when you just want
> "a class, any class" (i.e, as the return from a new()).

I think you missed this:

> =item 'OBJREF'
> 
> The subroutine was called in a context that requires an object reference
> be returned:
> 
> 	func()->method();

-Scott
-- 
Jonathan Scott Duff
[email protected]