74
Sub Signatures Next Steps Peter Martini github.com/PeterMartini [email protected] @PeterCMartini

Sub Signatures: Next Steps

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Sub Signatures: Next Steps

Sub SignaturesNext Steps

Peter Martinigithub.com/PeterMartini

[email protected]@PeterCMartini

Page 2: Sub Signatures: Next Steps

We did it!

Page 3: Sub Signatures: Next Steps

use feature 'signatures'now available in 5.20

Thanks @rjbs and Zefram

Page 4: Sub Signatures: Next Steps

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

Page 5: Sub Signatures: Next Steps

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

Page 6: Sub Signatures: Next Steps

What's in 5.20?

Page 7: Sub Signatures: Next Steps

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

no warnings 'experimental';

Page 8: Sub Signatures: Next Steps

Named Parameters

sub foo($bar, $baz)

Page 9: Sub Signatures: Next Steps

Anonymous Parameters

sub foo($, $baz)

Page 10: Sub Signatures: Next Steps

Default Values

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

Page 11: Sub Signatures: Next Steps

Default Values(but only for scalars)

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

Page 12: Sub Signatures: Next Steps

Strict Checking

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

Page 13: Sub Signatures: Next Steps

Strict Checking

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

Page 14: Sub Signatures: Next Steps

With Attributes:

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

Page 15: Sub Signatures: Next Steps

How it works:

Page 16: Sub Signatures: Next Steps

Perl, while parsing, rewritessignatures into equivalent

opcodes

Page 17: Sub Signatures: Next Steps

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]; ();}

Page 18: Sub Signatures: Next Steps

After parsing, there is notrace left of what the

signature was

Page 19: Sub Signatures: Next Steps

What's contentious in 5.20?

Page 20: Sub Signatures: Next Steps

In 5.18 and before:

sub CONSTANT(){ 5 }

Replaced by a constant atruntime

Page 21: Sub Signatures: Next Steps

In 5.20, it becomes:

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

Page 22: Sub Signatures: Next Steps

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

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

Page 23: Sub Signatures: Next Steps

sub foo($bar); # Syntax error

Page 24: Sub Signatures: Next Steps

No path to makinguse feature 'signatures';

a default

Page 25: Sub Signatures: Next Steps

This is why it's stillexperimental

Page 26: Sub Signatures: Next Steps

Goals for 5.22:

Page 27: Sub Signatures: Next Steps

Get past experimental!

Page 28: Sub Signatures: Next Steps

Revisit signature placement

Page 29: Sub Signatures: Next Steps

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

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

Page 30: Sub Signatures: Next Steps

Having the signature afterthe attributes can cause

subtle bugs

Page 31: Sub Signatures: Next Steps

Having the signature afterthe attributes makes

coexistence of prototypesand signatures much harder

Page 32: Sub Signatures: Next Steps

Especially problematic for

sub CONSTANT() {}

Page 33: Sub Signatures: Next Steps

Make signatures faster

Page 34: Sub Signatures: Next Steps

signatures right now dono more than rewrite your

code

Page 35: Sub Signatures: Next Steps

Which means it is exactlyas fast as doing it by hand

Page 36: Sub Signatures: Next Steps

This does not have to bethe case

Page 37: Sub Signatures: Next Steps

Preserve the signature

Page 38: Sub Signatures: Next Steps

Not just for aesthetics;preserving the signature

allows sub CONSTANT(){}to work again

Page 39: Sub Signatures: Next Steps

Among other optimizations

Page 40: Sub Signatures: Next Steps

Tweak error handling

Page 41: Sub Signatures: Next Steps

signatures right now injectsa 'die' into the sub if

the arguments don't match

Page 42: Sub Signatures: Next Steps

Which means the error isreported in the sub, not the

caller

Page 43: Sub Signatures: Next Steps

This is fixable for 5.22

Page 44: Sub Signatures: Next Steps

Possible enhancements for5.22

Page 45: Sub Signatures: Next Steps

A separate feature to havesignatures imply prototypes

Page 46: Sub Signatures: Next Steps

Having a compatibility modefor signatures would allow

safely* turning it on by default

Page 47: Sub Signatures: Next Steps

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

CPAN modules that hijackprototypes would have to be

modified to turn off signatures

Page 48: Sub Signatures: Next Steps

Allow signatures in subdeclarations

Page 49: Sub Signatures: Next Steps

Aside:

signaturesv.

initialization block

Page 50: Sub Signatures: Next Steps

Defaults in declarations isa sticky subject

Page 51: Sub Signatures: Next Steps

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

Page 52: Sub Signatures: Next Steps

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

Page 53: Sub Signatures: Next Steps

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

Page 54: Sub Signatures: Next Steps

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

Page 55: Sub Signatures: Next Steps

Fatalize redefinition

Page 56: Sub Signatures: Next Steps

Not necessary, but enablesoptimizations

Page 57: Sub Signatures: Next Steps

(sub redefinition alreadybreaks prototypes)

Page 58: Sub Signatures: Next Steps

Long term enhancements

Page 59: Sub Signatures: Next Steps

Particularly things that canbenefit from core

Page 60: Sub Signatures: Next Steps

Readonly args

sub foo($bar : ro)

Page 61: Sub Signatures: Next Steps

Native types

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

Page 62: Sub Signatures: Next Steps

Package types

sub foo(Some::Class $bar)

Page 63: Sub Signatures: Next Steps

Calling parameters by name

Page 64: Sub Signatures: Next Steps

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

This could be made as fastas simple assignment

Page 65: Sub Signatures: Next Steps

Aliasing

Page 66: Sub Signatures: Next Steps

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

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

Page 67: Sub Signatures: Next Steps

Implicit $self

(method keyword? There'sa method attribute already)

Page 68: Sub Signatures: Next Steps

Custom handler for subredefinition

Page 69: Sub Signatures: Next Steps

Which would allowmulti methods on CPAN

Page 70: Sub Signatures: Next Steps

Retrieve signature as a corefunction

Page 71: Sub Signatures: Next Steps

API to override signatureparsing

Page 72: Sub Signatures: Next Steps

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

for your sanity and its speed

Page 73: Sub Signatures: Next Steps

Questions?

Page 74: Sub Signatures: Next Steps

Thank you for coming