Raku / problem-solving

🦋 Problem Solving, a repo for handling problems that require review, deliberation and possibly debate
Artistic License 2.0
70 stars 16 forks source link

Map parameterized vs Hash parameterized #223

Open fecundf opened 4 years ago

fecundf commented 4 years ago

I like this

my $match-messages = Hash[Str, Regex].new( / <[aeiou]> / => 'vowel', / \d / => 'digit' );

because it constructs a Hash with keys & values just the way I like them.

I don't like that trying to do similar with a Map, my attempt at making an immutable lookup, gives me an error

my $match-messages = Map[Str, Regex].new( / <[aeiou]> / => 'vowel', / \d / => 'digit' );

Neither of these are documented. I'm opening a doc issue with Hash on the assumption that parameterized Hash construction is meant to be part of the language, but I'm not entirely sure. So the issue is:

Which classes that do Associative are meant to be parameterized? Which ones aren't?

fecundf commented 4 years ago

My guess is that Hash needs to be parameterized, as Rakudo 2020-07 has it now, to support declarations like

my Str %search-message{ Regex };

and since a Hash is a mutable Map, then I expect Map to be parameterized the same way.

Classes that derive from QuantHash would need a parameterized key, but not value. That seems to be the case in current Rakudo but not enshrined in the language spec, as far as I can tell. eg. this works but isn't documented explicitly

my %freq := Bag[ Regex ].new( )
fecundf commented 4 years ago

Now that I think about it, QuantHash values could be usefully parameterized too. What if you need your Bag to count via atomicint? Or have a subtype of Int that throws an exception when it's greater than 11?

subset This-Int-Goes-To-Eleven of Int where { $_ <= 11 or die "This goes to 11"}
my %freq := Bag[ This-Int-Goes-To-Eleven, Regex ].new( )
fecundf commented 4 years ago

... can type parameters be named? something like

 Bag[ :value[This-Int-Goes-To-Eleven] ].new()
 Bag[ :key[Regex] ].new()

Asking for the cases where one wants to set only the key type, leaving the value type as the default.