Re: Hash Constraints w/in subset's

2020-02-01 Thread Paul Procacci
Thanks for all the answers.  This will get me going.
I was unaware of the ' Hash[Int, Str]' type of declarative syntax.
I knew Hash[Str] was a thing but was unaware of the '[Int,Str]' type of
syntax.

Thanks for the insight!

On Sat, Feb 1, 2020 at 11:16 AM Elizabeth Mattijsen  wrote:

> subset PhoneNumber of Int where *.chars == 9;  # or any other
> constraint
> constant PhoneBook = Hash[PhoneNumber, Str];   # no need to make a
> class, a type suffices
>
> my %pb is PhoneBook = foo => 123456789;
> dd %pb;  # (my PhoneNumber %{Str} = :foo(123456789))



-- 
__

:(){ :|:& };:


Re: Hash Constraints w/in subset's

2020-02-01 Thread Elizabeth Mattijsen
subset PhoneNumber of Int where *.chars == 9;  # or any other constraint
constant PhoneBook = Hash[PhoneNumber, Str];   # no need to make a class, a 
type suffices

my %pb is PhoneBook = foo => 123456789;
dd %pb;  # (my PhoneNumber %{Str} = :foo(123456789))

Re: Hash Constraints w/in subset's

2020-02-01 Thread Brad Gilbert
I wouldn't use a subset like this:

subset PhoneBook of Hash[Int, Str];

I would instead use a constant:

constant PhoneBook = Hash[Int, Str];

Then you can use that for creating new instances.

PhoneBook.new(); # Hash[Int, Str].new()
# or
my %pb is PhoneBook; # my Int %pb{Str};

I might even consider creating a class:

class PhoneBook is Hash[Int, Str] {
}

my %pb is PhoneBook;

---

Both the constant and new class have the benefit that you can state in your
code what the %pb variable is actually used for.

Even better is that if you use the constant consistently, you can
transition to the class style easily.
Which is useful if you want to add methods.

---

Note that none of the above allow you to use it like this:

sub foo ( PhoneBook %pb ){…}

You have to do one of these:

sub foo ( PhoneBook $pb ){…}
sub foo ( PhoneBook \pb ){…}

sub foo ( Int %pb{Str} ){…} # only with constant or subset

On Sat, Feb 1, 2020 at 3:25 AM Tobias Boege  wrote:

> On Sat, 01 Feb 2020, Paul Procacci wrote:
> > Hey ladies/gents,
> >
> > How would one go about defining a subset of a Hash who's key's and values
> > are both constrained by something 
> >
> > I've read https://docs.perl6.org/type/Hash and it does make mention of
> > constraining keys and values, but not within the context of a subset.
> > Before I go ripping my hair out, I want to make sure I'm doing things
> right.
> >
> > For instance, what if I want to define a subset that uses a Hash as it's
> > base class, in which I want to add the constraint that it's keys must be
> > strings yet it's values are constrained to only accept Int's?
> >
>
> You would make a subset of Hash if you are getting Hashes and you want
> to give some common code that checks the validity of the Hash a name.
> If you want a new type that you can actually create objects from, the
> solution is subclassing, not subsetting, but works similarly to below.
> (Just to make sure you're not surprised when you can't call .new on
> your subset later.)
>
> With a subset you could do:
>
> subset PhoneBook of Hash[Int, Str];
>
> my Int %pb{Str};
> %pb   = +49_123_4567_6789;  # good
> try %pb = "unknown";  # fails
>
> dd %pb  #= Hash[Int,Str] %pb = (my Int %{Str} = :tobias(4912345676789))
> say %pb ~~ PhoneBook  #= True
>
> to the same effect.
>
> Note that if you subset PhoneBook of Hash[Int, Str], then your hashes
> really must be typed. An ordinary Hash that just happens to always map
> strings to integers is not sufficient and will fail the type check.
> You could make a looser subset of Hash with a where clause that checks
> compatibility of all keys with Str and of all values with Int as well,
> but that would be more expensive.
>
> To answer your question: Hash does the Associative role which is
> parameterized by the key and value type, but *in reverse order*.
> Hash[Int, Str] is the type that maps Int <-- Str, just like in the
> neat `my Int %pb{Str}` declaration.
>
> Regards,
> Tobias
>
> --
> "There's an old saying: Don't change anything... ever!" -- Mr. Monk
>


Re: Hash Constraints w/in subset's

2020-02-01 Thread Tobias Boege
On Sat, 01 Feb 2020, Paul Procacci wrote:
> Hey ladies/gents,
> 
> How would one go about defining a subset of a Hash who's key's and values
> are both constrained by something 
> 
> I've read https://docs.perl6.org/type/Hash and it does make mention of
> constraining keys and values, but not within the context of a subset.
> Before I go ripping my hair out, I want to make sure I'm doing things right.
> 
> For instance, what if I want to define a subset that uses a Hash as it's
> base class, in which I want to add the constraint that it's keys must be
> strings yet it's values are constrained to only accept Int's?
> 

You would make a subset of Hash if you are getting Hashes and you want
to give some common code that checks the validity of the Hash a name.
If you want a new type that you can actually create objects from, the
solution is subclassing, not subsetting, but works similarly to below.
(Just to make sure you're not surprised when you can't call .new on
your subset later.)

With a subset you could do:

subset PhoneBook of Hash[Int, Str];

my Int %pb{Str};
%pb   = +49_123_4567_6789;  # good
try %pb = "unknown";  # fails

dd %pb  #= Hash[Int,Str] %pb = (my Int %{Str} = :tobias(4912345676789))
say %pb ~~ PhoneBook  #= True

to the same effect.

Note that if you subset PhoneBook of Hash[Int, Str], then your hashes
really must be typed. An ordinary Hash that just happens to always map
strings to integers is not sufficient and will fail the type check.
You could make a looser subset of Hash with a where clause that checks
compatibility of all keys with Str and of all values with Int as well,
but that would be more expensive.

To answer your question: Hash does the Associative role which is
parameterized by the key and value type, but *in reverse order*.
Hash[Int, Str] is the type that maps Int <-- Str, just like in the
neat `my Int %pb{Str}` declaration.

Regards,
Tobias

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk


Hash Constraints w/in subset's

2020-02-01 Thread Paul Procacci
Hey ladies/gents,

How would one go about defining a subset of a Hash who's key's and values
are both constrained by something 

I've read https://docs.perl6.org/type/Hash and it does make mention of
constraining keys and values, but not within the context of a subset.
Before I go ripping my hair out, I want to make sure I'm doing things right.

For instance, what if I want to define a subset that uses a Hash as it's
base class, in which I want to add the constraint that it's keys must be
strings yet it's values are constrained to only accept Int's?

Thanks in Advance!

-- 
__

:(){ :|:& };: