Sub Signatures: Next Steps

Preview:

DESCRIPTION

YAPC::NA 2014 talk slides for Sub Signatures: Next Steps

Citation preview

Sub SignaturesNext Steps

Peter Martinigithub.com/PeterMartini

PeterCMartini@GMail.com@PeterCMartini

We did it!

use feature 'signatures'now available in 5.20

Thanks @rjbs and Zefram

1) Review last year's talk2) What's in 5.20

3) What's contentious in 5.204) Potential enhancements for 5.22

5) Long term enhancements

Goals from last year's talk:Add API for get/fetch – XAdd API for parsing – XAdd a new module – ✓

Update CPAN signatures – XPerformance boost – X

What's in 5.20?

All of the following assumes:use feature 'signatures';

no warnings 'experimental';

Named Parameters

sub foo($bar, $baz)

Anonymous Parameters

sub foo($, $baz)

Default Values

my $n = 1;sub update($rec, $id = $n++)

Default Values(but only for scalars)

sub nope(@arr = (1,2));# syntax error

Strict Checking

sub twoargs($one, $two) {}twoargs(1); # dies

Strict Checking

sub usedef($one, $two=2) {}usedef(1); # lives!

With Attributes:

sub foo :prototype($) ($bar){}

How it works:

Perl, while parsing, rewritessignatures into equivalent

opcodes

perl -MO=Deparse -Mfeature=signatures -e 'sub foo($bar){}'

The signatures feature is experimental at -e line 1.sub foo { use feature 'signatures'; die 'Too many arguments for subroutine' unless @_ <= 1; die 'Too few arguments for subroutine' unless @_ >= 1; my $bar = $_[0]; ();}

After parsing, there is notrace left of what the

signature was

What's contentious in 5.20?

In 5.18 and before:

sub CONSTANT(){ 5 }

Replaced by a constant atruntime

In 5.20, it becomes:

sub CONSTANT { use feature 'signatures'; die 'Too many arguments for subroutine' unless @_ <= 0; (); 5;}

sub foo : lvalue ($bar) {}# Legal

sub foo : lvalue($bar) {}# Syntax error

sub foo($bar); # Syntax error

No path to makinguse feature 'signatures';

a default

This is why it's stillexperimental

Goals for 5.22:

Get past experimental!

Revisit signature placement

Right now we have:sub <name> <proto> <attr>

And:sub <name> <attr> <sig>

Having the signature afterthe attributes can cause

subtle bugs

Having the signature afterthe attributes makes

coexistence of prototypesand signatures much harder

Especially problematic for

sub CONSTANT() {}

Make signatures faster

signatures right now dono more than rewrite your

code

Which means it is exactlyas fast as doing it by hand

This does not have to bethe case

Preserve the signature

Not just for aesthetics;preserving the signature

allows sub CONSTANT(){}to work again

Among other optimizations

Tweak error handling

signatures right now injectsa 'die' into the sub if

the arguments don't match

Which means the error isreported in the sub, not the

caller

This is fixable for 5.22

Possible enhancements for5.22

A separate feature to havesignatures imply prototypes

Having a compatibility modefor signatures would allow

safely* turning it on by default

safely* - code that warnedabout illegal prototypeswould now probably die;

CPAN modules that hijackprototypes would have to be

modified to turn off signatures

Allow signatures in subdeclarations

Aside:

signaturesv.

initialization block

Defaults in declarations isa sticky subject

# This is badour $bar;sub foo($baz = $bar);…{ my $bar = 5; sub foo($baz = $bar) {}}

# This isn't any better!sub foo($baz);…{ my $bar = 5; sub foo($baz = $bar) {}}

# This one's sticky ...sub foo($);…{ my $bar = 5; sub foo($baz = $bar) {}}

# Maybe this?sub foo($baz = ...);…{ my $bar = 5; sub foo($baz = $bar) {}}

Fatalize redefinition

Not necessary, but enablesoptimizations

(sub redefinition alreadybreaks prototypes)

Long term enhancements

Particularly things that canbenefit from core

Readonly args

sub foo($bar : ro)

Native types

sub foo(int $a, num $b, str $c)

Package types

sub foo(Some::Class $bar)

Calling parameters by name

sub foo (-bar, -baz = 2) {}foo(-bar => 1);

This could be made as fastas simple assignment

Aliasing

sub foo($bar : alias) { $bar = “New”;}

my $var = “Old”;foo($var);# $var is now “New”

Implicit $self

(method keyword? There'sa method attribute already)

Custom handler for subredefinition

Which would allowmulti methods on CPAN

Retrieve signature as a corefunction

API to override signatureparsing

The goal is to let youmake promises to Perl,which Perl can enforce

for your sanity and its speed

Questions?

Thank you for coming

Recommended