Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Perl: porters

RFC: Objective-C style "undef accepts anything"

 

 

Perl porters RSS feed   Index | Next | Previous | View Threaded


mcmahon at ibiblio

May 26, 2012, 5:21 PM

Post #1 of 4 (67 views)
Permalink
RFC: Objective-C style "undef accepts anything"

One of the things I've discovered in the process of writing Objective-C
code is that its convention that nil (the null pointer) will accept any
message - but will do nothing in response - vastly simplifies code.

I'm wondering if a pragma to enable this would be a good idea for Perl.

Advantages:
- eliminates a lot of "if (defined $foo) { ..." checking. If $foo is
defined anfd yo send a message to it, then that goes to the object
associated (unless of course $foo isn't an object). Less code is better
code.

Disadvantages:
- As implemented in Objective-C, the messages sent to nil simply do
nothing, and there's no notification that the message went to nil. Adding a
warning pragma for this is an option.

Finer points need to be worked out; basically I can see the result of
sending a message to undef to return undef; that will work in most
situations. In list context, I suppose it should return an empty list.

If this seems like an idea, I can invest some time in putting together a
reference implementation that could be tried out. Also, if it doesn't seem
like a good idea to discuss it here, I'l be happy to move this over to
Perlmonks.


zefram at fysh

May 29, 2012, 12:54 AM

Post #2 of 4 (64 views)
Permalink
Re: RFC: Objective-C style "undef accepts anything" [In reply to]

Joe McMahon wrote:
>Disadvantages:
> - As implemented in Objective-C, the messages sent to nil simply do
>nothing, and there's no notification that the message went to nil.

This loss of error checking makes it generally a bad idea. It'd be OK
to lose the error checking in a limited context, but to have different
method dispatch behaviour in different parts of the code will lead to
another problem: code that uses undef in this way will pass undef to
other code in situations where it's meant to provide an object that
handles a certain method. An undef crossing the boundary between the
two types of code will cause surprising incompatibility. Fundamentally,
this pragma would damage the universality of method calling semantics.

-zefram


fawaka at gmail

May 29, 2012, 2:26 AM

Post #3 of 4 (60 views)
Permalink
Re: RFC: Objective-C style "undef accepts anything" [In reply to]

On Sun, May 27, 2012 at 2:21 AM, Joe McMahon <mcmahon [at] ibiblio> wrote:
> One of the things I've discovered in the process of writing Objective-C code
> is that its convention that nil (the null pointer) will accept any message -
> but will do nothing in response - vastly simplifies code.
>
> I'm wondering if a pragma to enable this would be a good idea for Perl.
>
> Advantages:
>  - eliminates a lot of "if (defined $foo) { ..." checking. If $foo is
> defined anfd yo send a message to it, then that goes to the object
> associated (unless of course $foo isn't an object). Less code is better
> code.
>
> Disadvantages:
>  - As implemented in Objective-C, the messages sent to nil simply do
> nothing, and there's no notification that the message went to nil. Adding a
> warning pragma for this is an option.
>
> Finer points need to be worked out; basically I can see the result of
> sending a message to undef to return undef; that will work in most
> situations. In list context, I suppose it should return an empty list.
>
> If this seems like an idea, I can invest some time in putting together a
> reference implementation that could be tried out. Also, if it doesn't seem
> like a good idea to discuss it here, I'l be happy to move this over to
> Perlmonks.

I think this is easily implementable using autobox.

Leon


nick at ccl4

May 31, 2012, 9:03 AM

Post #4 of 4 (61 views)
Permalink
Re: RFC: Objective-C style "undef accepts anything" [In reply to]

On Tue, May 29, 2012 at 11:26:24AM +0200, Leon Timmermans wrote:
> On Sun, May 27, 2012 at 2:21 AM, Joe McMahon <mcmahon [at] ibiblio> wrote:
> > One of the things I've discovered in the process of writing Objective-C code
> > is that its convention that nil (the null pointer) will accept any message -
> > but will do nothing in response - vastly simplifies code.
> >
> > I'm wondering if a pragma to enable this would be a good idea for Perl.

> > If this seems like an idea, I can invest some time in putting together a
> > reference implementation that could be tried out. Also, if it doesn't seem
> > like a good idea to discuss it here, I'l be happy to move this over to
> > Perlmonks.
>
> I think this is easily implementable using autobox.

Yes, this was what I was wondering - what hooks is the core missing to make
it possible to do this entirely in a CPAN module?

Attempting to build a reference implementation would be a useful way to
answer this.

Nicholas Clark

Perl porters RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.