Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Type system

Reini Urban edited this page Jan 7, 2016 · 27 revisions

See the perltypes documentation from master, and the latest version is usually in the feature/gh7-signatures or featurex/gh16-multi branch.

In the featurex/gh7-sigs+libs branch several libraries are already "modernized", i.e. converted to typed signatures.

See for example the modernization of Test-Simple in commit 2d39610cb5.

The translation is pretty straightforward:

-sub is_eq {
-    my( $self, $got, $expect, $name ) = @_;
+sub is_eq ( $self, $got?, $expect?, $name?) {

The ? suffix from perl6 denotes an optional argument. If left out in the call, the value will be undef. There's no arity inspection as in @_.

$self is still in the signature. When is_eq is declared as method and not as sub, $self is not needed anymore and should be omitted. This is currently in work in the feature/gh16-multi branch.

Now to the types

-sub skip {
-    my( $self, $why, $name ) = @_;
-    $why ||= '';
-    $name = '' unless defined $name;
+sub skip ( $self, str $why='', $name? ) {
     $self->_unoverload_str( \$why );

$why is declared with the str coretype. str is the unboxed variant of the perl5-level boxed Str type, same as in Perl6. That means the compiler accepts only strings, not other scalars, and only strings which are known to the compiler at compile-time. Those strings will then be optimized to fast unboxed variants if possible or not, as the compiler sees fit.

$name? should have also been declared as str $name=undef instead. This is currently valid code I think, but inconsistent with the current type system. undef is no str type. It really should be str? $name?, str? denoting str or the Undef type.

Clone this wiki locally