develooper Front page | perl.perl6.language | Postings from September 2001

Re: Prototypes

Thread Previous | Thread Next
From:
Bryan C . Warnock
Date:
September 2, 2001 19:15
Subject:
Re: Prototypes
Message ID:
0109022211590B.05284@wakko.idiocity.nut
On Sunday 02 September 2001 07:49 pm, Dan Sugalski wrote:
> On Sun, 2 Sep 2001, Bryan C. Warnock wrote:
> > Are prototypes going to be checked at runtime now?
>
> For methods, at least. Dunno about subs, that's Larry's call. I could make
> a good language case for and against it. It adds overhead on sub calls,
> which is a bad thing generally. (I'd be OK with the declaration "All
> prototyped subs must have their prototypes known before the BEGIN phase is
> done"... :)

Well, here's a simple, yet stupid, idea.  (And I'd be content with having 
prototype declarations being a compile-time only requirement, too.)

Code injection, a la Perl's -p and -n switch.

sub foo (protoype) {
    code
}

is compiled as

sub foo { {
    my $proto = prototype;
    # prototype checking code here.  Must be non-destructive on @_
    }
    code
}

- It only affects the specific subroutines that declared prototypes in the 
first place.  

- With the ability to inject lexical variables up a scope, prototype 
assignment would work...

sub foo (my $bar, my Dog $baz, my @array) {
    # $bar, $baz, and @array are all visible
}

- It's certainly pluggable.  It could even be written in Perl.  That means 
powerful prototyping capabilities.

- Automagically works with named subs and code references, subroutine 
replacement a la 'goto &cref', and any other way you could think of calling 
the subroutine.

- If the subroutine is ever replaced, the prototypes magically go away.  (Or 
are replaced by ones in the new code.)

- It could be standard enough (from an opcode generation perspective, that 
B::Deparse would be able to reconstruct the original subroutine code.

-- 
Bryan C. Warnock
bwarnock@capita.com

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About