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

subs and the type system

Thread Next
From:
Theo van den Heuvel
Date:
July 20, 2020 10:53
Subject:
subs and the type system
Message ID:
21bccc74d558e5007b30f0a57a0852ad@heuvelhlt.nl
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 
signature.
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 
solution.

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 
roles.
I hope to say soemething like

sub walker(Walkable &callback, ...)

What am I missing here?

Thanks,


-- 
Theo van den Heuvel
Van den Heuvel HLT Consultancy

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About