How about separating dynamic world and static world?

I write comment before topic. Please see it.


Current subroutine signatures implementation contains two features which purposes are different

My opinion is that argument count checking by subroutine signature is not fit to Perl because Perl is dynamic language.

Perl is dynamic language, so argument count checking is done in run-time, not in compile time. This damage to performance of subroutine call.

Perl one big problem is that subroutine call is slow. If this is improved, Perl have big benefit.

"sub foo($x, $y) { ... }" should be faster than "sub foo { my ($x, $y) = @_ }"

Many existing projects will use subroutine signatures not worrying about performance damage.

How about separating dynamic world and static world?

By the way, I am reading cperl specification by Reini Urban. This is very interesting.

He want to introduce many static features to Perl. This is mainly performance reason.

I think this is good, but a little messy for current perl. mixing dynamic features and static features is not good. Perl 6 mix dynamic features and static features. but This is a little messy for users.

My proposal is that how about separating dynamic world and static world. Static features is used only in static world. Static features are, for example, type checking, argument count checking, inline expansion of method call, typed array.

Static world is that

package Foo : static;

# Inheritance is forbidden for performance of compile time

# Attributes. This is static continuous spaces, not hash reference.
has x : IV; # Native type
has y : NV; # Native type
has z : NV[]; # Array is Reference

# Multicall is default. Type and argument count is checked
# new is special keyword for object initialization
sub new ($self : Foo, $x : IV) {
$self->{x} = $x;
$self->{y} = 0;
}
sub new ($self : Foo, $x : IV, $y : NV) {
$self->{x} = $x;
$self->{y} = $y;
}

sub total($self: Foo) : NV {
my $total : IV;

$total = $self->{x} + $self->{y};

return $total;
}

package Bar : static;

# can use ffi in static package
extern sub twite($x : IV) : IV;

4 Comments

Perl 6 mix dynamic features and static features. but This is a little messy for users.

Care to elaborate or show an example instead of using argument by assertion?

First is normal subroutine. This is dynamic language feature. Last two are function overloading. This is static language feature.

Function overloading is indeed a static language feature.

But the multi keyword doesn't provide static function overloading.
The multi keyword provides dynamic multiple dispatch.

Function overloading occurs at compile-time, when the compiler looks at the static types of the declared arguments to an overloaded subroutine or method and selects which overloading to call on the basis of those compile-time types.

Multiple dispatch occurs at run-time, when the interpreter looks at the dynamic types of the actual arguments to a multisub or multimethod, and selects which variant to call on the basis of those run-time types.

(I originally explained the important difference between the two concepts in one of the original Perl 6 Exegesis documents.)

So the multi keyword is a perfectly appropriate addition to dynamic languages like Perl 6, and maybe even eventually to Perl 5.

Indeed, you can use multiple dispatch now in Perl 5 via the Class::Multimethods::Pure or Kavorka CPAN modules, and soon via the (next release of) the Dios module as well.

Leave a comment

About Yuki Kimoto

user-pic I'm Perl Programmer. I LOVE Perl. I want to contribute Perl community and Perl users.