develooper Front page | perl.perl5.porters | Postings from March 2016

sub signature and lexical types

Thread Next
Dave Mitchell
March 23, 2016 14:48
sub signature and lexical types
Message ID:
One thing I'd like to add to subroutine signatures is optional types.

Perl already supports

    my Foo $x;

which currently isn't made much use of in core, apart from with 'use fields',
which allows a lexical holding a hash ref to have compile-time and
run-time checks on allowed hash keys.

Adding this to signatures should be a no-brainer. I propose that the
existing signature syntax be extended so that every $/@/%var can be
prefixed with an optional type, with the same semantics as existing
my declarations, e.g.

    sub f(Int $i, Foo::Bar $fb, $plain) { ...}

Which would be semantically equivalent to

    sub f {
        my Int $i       = $_[0];
        my Foo::Bar $fb = $_[1],
        my $plain       = $_[2];
        # ... plus arity checks, default values etc....

This would be still be a compile-time error:

    sub f(Int $) {...}

since it's a placeholder and not a variable.

I'm not currently proposing any new semantics for such typed variables.

The Enterprise successfully ferries an alien VIP from one place to another
without serious incident.
    -- Things That Never Happen in "Star Trek" #7

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About