develooper Front page | perl.perl6.users | Postings from July 2020

subs and the type system

Thread Next
Theo van den Heuvel
July 20, 2020 10:53
subs and the type system
Message ID:
Hi gurus,

after looking at the documentation on Sub, Signature and the raku type 
system I find myself unable to constrain the types of functions in the 
way I think I need.

The situation: I have a function, let's call in 'walker', whose first 
parameter is a callback.
I wish to express that only callbacks with a certain Signature and 
return type are acceptable.
Let's say the callback should follow :(Numeric $n --> Numeric). My 
callback is going to be more complicated, but for simplicity sake.

I was hoping to be able to use a named type of function with this 
I know I can say

my $sig =  :(Numeric $n --> Numeric);

but my attempts to use $sig fail. I found no example of an application 
beyond smartmatching.

What I tried so far:
sub walker(&num2num:$sig, ...) { ... }

is syntactically incorrect and yields:
   Can only use the : invocant marker in the signature for a method
and only takes a single step in the direction of a more abstract 

I know I can say
sub walker(&num2num:(Numeric $n --> Numeric), ...) { ... }

but I was hoping for an explicit type, maybe using class, or subset or 
I hope to say soemething like

sub walker(Walkable &callback, ...)

What am I missing here?


Theo van den Heuvel
Van den Heuvel HLT Consultancy

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