Recently this Perl 6 version of the factorial function was mentioned:

  sub postfix:<!>(Int $n) { [*] 2 .. $n }

I experimented a bit with it and found that I couldn't do

  '3'!

as I naively expected from my Perl 5 intuition.  Similarly, I can't pass an 
integer literal to a function that takes a Str.  String/number transparency 
isn't as total as it used to be.

So now there seems to be some tension in code design.  If I want to freely 
accept both numbers and strings in the manner of Perl 5, it looks like I must 
type all of my function arguments as Cool, or omit the types altogether.  But 
doing so removes helpful information from the function signature.  The argument 
to the factorial is conceptually an Int, but I can't advertise that fact in the 
signature if I want to be able to accept a 
string-which-will-be-converted-to-an-Int.

In my tinkering around I've all but come to the conclusion that I should just 
get used to performing conversions manually, eg:

  '1 2 3'.split(/' '/).map((+*)!)

...even though that feels unpleasantly Pythonic (if more concise).  But first I 
wanted to check in here and see if there's some middle way that perhaps I 
haven't been seeing.

Reply via email to