modules and subsets

Hi All,

If I create a subset in the global section of a module
(not inside a sub), can the subset be seen by the
main program?

Can a subset be declared on a sub's declaration line?
(Hope springs eternal.)

Many thanks,
-T
0
perl6
12/11/2019 5:46:37 PM
perl.perl6.users 1388 articles. 0 followers. Follow

11 Replies
69 Views

Similar Articles

[PageSpeed] 11

--0000000000007e5c7b059971537e
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Subsets follow pretty much the same rules as every other declared thing.
Change subset by "variable" or "class", if the answer is true, it's also
true for subsets.

By default, the scope of anything (containers, classes, whatever) is
lexical to the scope they are in.

El mi=C3=A9., 11 dic. 2019 a las 18:48, ToddAndMargo via perl6-users (<
perl6-users@perl.org>) escribi=C3=B3:

> Hi All,
>
> If I create a subset in the global section of a module
> (not inside a sub), can the subset be seen by the
> main program?
>
> Can a subset be declared on a sub's declaration line?
> (Hope springs eternal.)
>
> Many thanks,
> -T
>


--=20
JJ

--0000000000007e5c7b059971537e
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>Subsets follow pretty much the same rules as every ot=
her declared thing. Change subset by &quot;variable&quot; or &quot;class&qu=
ot;, if the answer is true, it&#39;s also true for subsets.</div><div><br><=
/div><div>By default, the scope of anything (containers, classes, whatever)=
 is lexical to the scope they are in.<br></div></div><br><div class=3D"gmai=
l_quote"><div dir=3D"ltr" class=3D"gmail_attr">El mi=C3=A9., 11 dic. 2019 a=
 las 18:48, ToddAndMargo via perl6-users (&lt;<a href=3D"mailto:perl6-users=
@perl.org">perl6-users@perl.org</a>&gt;) escribi=C3=B3:<br></div><blockquot=
e class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px s=
olid rgb(204,204,204);padding-left:1ex">Hi All,<br>
<br>
If I create a subset in the global section of a module<br>
(not inside a sub), can the subset be seen by the<br>
main program?<br>
<br>
Can a subset be declared on a sub&#39;s declaration line?<br>
(Hope springs eternal.)<br>
<br>
Many thanks,<br>
-T<br>
</blockquote></div><br clear=3D"all"><br>-- <br><div dir=3D"ltr" class=3D"g=
mail_signature">JJ</div>

--0000000000007e5c7b059971537e--
0
jjmerelo
12/11/2019 5:54:43 PM
--00000000000078c09f0599715eca
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

El mi=C3=A9., 11 dic. 2019 a las 18:54, JJ Merelo (<jjmerelo@gmail.com>)
escribi=C3=B3:

> Subsets follow pretty much the same rules as every other declared thing.
> Change subset by "variable" or "class", if the answer is true, it's also
> true for subsets.
>
> By default, the scope of anything (containers, classes, whatever) is
> lexical to the scope they are in.
>

Sorry, that's not true. Classes have package scope, not lexical scope, by
default; that is, they are "our": https://docs.perl6.org/language/classtut

Subsets... well I don't know. I would say they are package scoped by
default, same as classes.

JJ

--00000000000078c09f0599715eca
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr"><br></div><br><div class=3D"gmail_quote">=
<div dir=3D"ltr" class=3D"gmail_attr">El mi=C3=A9., 11 dic. 2019 a las 18:5=
4, JJ Merelo (&lt;<a href=3D"mailto:jjmerelo@gmail.com">jjmerelo@gmail.com<=
/a>&gt;) escribi=C3=B3:<br></div><blockquote class=3D"gmail_quote" style=3D=
"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-le=
ft:1ex"><div dir=3D"ltr"><div>Subsets follow pretty much the same rules as =
every other declared thing. Change subset by &quot;variable&quot; or &quot;=
class&quot;, if the answer is true, it&#39;s also true for subsets.</div><d=
iv><br></div><div>By default, the scope of anything (containers, classes, w=
hatever) is lexical to the scope they are in.<br></div></div></blockquote><=
div><br></div><div>Sorry, that&#39;s not true. Classes have package scope, =
not lexical scope, by default; that is, they are &quot;our&quot;: <a href=
=3D"https://docs.perl6.org/language/classtut">https://docs.perl6.org/langua=
ge/classtut</a></div><div><br></div><div>Subsets... well I don&#39;t know. =
I would say they are package scoped by default, same as classes.</div><div>=
<br></div><div>JJ<br></div></div></div>

--00000000000078c09f0599715eca--
0
jjmerelo
12/11/2019 5:57:47 PM
$ 6 'subset A of Int; dd OUR::.keys'
("A",).Seq


> On 11 Dec 2019, at 18:57, JJ Merelo <jjmerelo@gmail.com> wrote:
>=20
>=20
>=20
> El mi=C3=A9., 11 dic. 2019 a las 18:54, JJ Merelo =
(<jjmerelo@gmail.com>) escribi=C3=B3:
> Subsets follow pretty much the same rules as every other declared =
thing. Change subset by "variable" or "class", if the answer is true, =
it's also true for subsets.
>=20
> By default, the scope of anything (containers, classes, whatever) is =
lexical to the scope they are in.
>=20
> Sorry, that's not true. Classes have package scope, not lexical scope, =
by default; that is, they are "our": =
https://docs.perl6.org/language/classtut
>=20
> Subsets... well I don't know. I would say they are package scoped by =
default, same as classes.
>=20
> JJ
0
liz
12/11/2019 6:16:50 PM
--0000000000007e0aea059971b740
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

File test.pm6

unit module test;
subset PosInt of Int is export where * > 0;


File test.p6

use lib '.';
use test;
sub mytest(PosInt $a) { say $a }
mytest(1);     # output: 1
mytest(-1);    # output Constraint type check failed in binding to
parameter '$a'; expected test::PosInt but got Int (-1)

On Wed, Dec 11, 2019 at 7:04 PM JJ Merelo <jjmerelo@gmail.com> wrote:

>
>
> El mi=C3=A9., 11 dic. 2019 a las 18:54, JJ Merelo (<jjmerelo@gmail.com>)
> escribi=C3=B3:
>
>> Subsets follow pretty much the same rules as every other declared thing.
>> Change subset by "variable" or "class", if the answer is true, it's also
>> true for subsets.
>>
>> By default, the scope of anything (containers, classes, whatever) is
>> lexical to the scope they are in.
>>
>
> Sorry, that's not true. Classes have package scope, not lexical scope, by
> default; that is, they are "our": https://docs.perl6.org/language/classtu=
t
>
> Subsets... well I don't know. I would say they are package scoped by
> default, same as classes.
>
> JJ
>


--=20
Fernando Santagata

--0000000000007e0aea059971b740
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_default" style=3D"color:#000000">File =
test.pm6</div><div class=3D"gmail_default" style=3D"color:#000000"><br></di=
v><div class=3D"gmail_default" style=3D"color:#000000">unit module test;<br=
>subset PosInt of Int is export where * &gt; 0;</div><div class=3D"gmail_de=
fault" style=3D"color:#000000"><br></div><div class=3D"gmail_default" style=
=3D"color:#000000"><br></div><div class=3D"gmail_default" style=3D"color:#0=
00000">File test.p6</div><div class=3D"gmail_default" style=3D"color:#00000=
0"><br></div><div class=3D"gmail_default" style=3D"color:#000000">use lib &=
#39;.&#39;;<br>use test;<br>sub mytest(PosInt $a) { say $a }<br>mytest(1);=
=C2=A0=C2=A0=C2=A0=C2=A0 # output: 1<br>mytest(-1);=C2=A0=C2=A0=C2=A0 # out=
put Constraint type check failed in binding to parameter &#39;$a&#39;; expe=
cted test::PosInt but got Int (-1)</div></div><br><div class=3D"gmail_quote=
"><div dir=3D"ltr" class=3D"gmail_attr">On Wed, Dec 11, 2019 at 7:04 PM JJ =
Merelo &lt;<a href=3D"mailto:jjmerelo@gmail.com">jjmerelo@gmail.com</a>&gt;=
 wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px =
0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir=
=3D"ltr"><div dir=3D"ltr"><br></div><br><div class=3D"gmail_quote"><div dir=
=3D"ltr" class=3D"gmail_attr">El mi=C3=A9., 11 dic. 2019 a las 18:54, JJ Me=
relo (&lt;<a href=3D"mailto:jjmerelo@gmail.com" target=3D"_blank">jjmerelo@=
gmail.com</a>&gt;) escribi=C3=B3:<br></div><blockquote class=3D"gmail_quote=
" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);=
padding-left:1ex"><div dir=3D"ltr"><div>Subsets follow pretty much the same=
 rules as every other declared thing. Change subset by &quot;variable&quot;=
 or &quot;class&quot;, if the answer is true, it&#39;s also true for subset=
s.</div><div><br></div><div>By default, the scope of anything (containers, =
classes, whatever) is lexical to the scope they are in.<br></div></div></bl=
ockquote><div><br></div><div>Sorry, that&#39;s not true. Classes have packa=
ge scope, not lexical scope, by default; that is, they are &quot;our&quot;:=
 <a href=3D"https://docs.perl6.org/language/classtut" target=3D"_blank">htt=
ps://docs.perl6.org/language/classtut</a></div><div><br></div><div>Subsets.=
... well I don&#39;t know. I would say they are package scoped by default, s=
ame as classes.</div><div><br></div><div>JJ<br></div></div></div>
</blockquote></div><br clear=3D"all"><br>-- <br><div dir=3D"ltr" class=3D"g=
mail_signature">Fernando Santagata</div>

--0000000000007e0aea059971b740--
0
nando
12/11/2019 6:22:40 PM

On 2019-12-11 09:54, JJ Merelo wrote:
> By default, the scope of anything (containers, classes, whatever) is 
> lexical to the scope they are in.

Hi JJ,

Thank you for responding.

By the way, I'd be a Golden.

I don't understand how you are using the word "lexical".

I may be mixing Modula2 with Raku in that I am thinking
I define the subset and some kind of structure in the global
section of the module and expecting the importer of the sub
also see it.  Is the structure defined in the declaration
line of the exported sub?

Many thanks,
-T
0
perl6
12/12/2019 2:26:28 AM

On 2019-12-11 10:16, Elizabeth Mattijsen wrote:
> $ 6 'subset A of Int; dd OUR::.keys'
> ("A",).Seq

Hi Elizabeth,

subset A of Int; dd OUR::.keys
("A",).Seq
Nil

Would you explain how your are using dd and what your
are showing me here?

Also how it ties into my original question as to if
the calling programs sees anything in the
global section of a module's sub.

Many thanks,
-T
0
perl6
12/12/2019 2:30:45 AM

On 2019-12-11 10:22, Fernando Santagata wrote:
> File test.pm6
>=20
> unit module test;
> subset PosInt of Int is export where * > 0;
>=20
>=20
> File test.p6
>=20
> use lib '.';
> use test;
> sub mytest(PosInt $a) { say $a }
> mytest(1);=C2=A0=C2=A0=C2=A0=C2=A0 # output: 1
> mytest(-1);=C2=A0=C2=A0=C2=A0 # output Constraint type check failed in bi=
nding to=20
> parameter '$a'; expected test::PosInt but got Int (-1)

Hi Fernando,

What response do you get back with?

     perl6 -c test.pl6

-T
0
ToddAndMargo
12/12/2019 2:33:05 AM
--000000000000b378af05997d7283
Content-Type: text/plain; charset="UTF-8"

On Thu, Dec 12, 2019 at 3:46 AM Todd Chester <ToddAndMargo@zoho.com> wrote:

> On 2019-12-11 10:22, Fernando Santagata wrote:
> > File test.pm6
> >
> > unit module test;
> > subset PosInt of Int is export where * > 0;
> >
> >
> > File test.p6
> >
> > use lib '.';
> > use test;
> > sub mytest(PosInt $a) { say $a }
> > mytest(1);     # output: 1
> > mytest(-1);    # output Constraint type check failed in binding to
> > parameter '$a'; expected test::PosInt but got Int (-1)
>
> Hi Fernando,
>
> What response do you get back with?
>
>      perl6 -c test.pl6
>
> -T
>

I don't understand the reason for your question, but anyway:

$ perl6 -c test.pl6
Syntax OK

-- 
Fernando Santagata

--000000000000b378af05997d7283
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail=
_attr">On Thu, Dec 12, 2019 at 3:46 AM Todd Chester &lt;<a href=3D"mailto:T=
oddAndMargo@zoho.com">ToddAndMargo@zoho.com</a>&gt; wrote:<br></div><blockq=
uote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1p=
x solid rgb(204,204,204);padding-left:1ex">
On 2019-12-11 10:22, Fernando Santagata wrote:<br>
&gt; File test.pm6<br>
&gt; <br>
&gt; unit module test;<br>
&gt; subset PosInt of Int is export where * &gt; 0;<br>
&gt; <br>
&gt; <br>
&gt; File test.p6<br>
&gt; <br>
&gt; use lib &#39;.&#39;;<br>
&gt; use test;<br>
&gt; sub mytest(PosInt $a) { say $a }<br>
&gt; mytest(1);=C2=A0=C2=A0=C2=A0=C2=A0 # output: 1<br>
&gt; mytest(-1);=C2=A0=C2=A0=C2=A0 # output Constraint type check failed in=
 binding to <br>
&gt; parameter &#39;$a&#39;; expected test::PosInt but got Int (-1)<br>
<br>
Hi Fernando,<br>
<br>
What response do you get back with?<br>
<br>
=C2=A0 =C2=A0 =C2=A0perl6 -c test.pl6<br>
<br>
-T<br>
</blockquote></div><br clear=3D"all"><div><div style=3D"color:rgb(0,0,0)" c=
lass=3D"gmail_default">I don&#39;t understand the reason for your question,=
 but anyway:</div><div style=3D"color:rgb(0,0,0)" class=3D"gmail_default"><=
br></div><div style=3D"color:rgb(0,0,0)" class=3D"gmail_default">$ perl6 -c=
 test.pl6</div><div style=3D"color:rgb(0,0,0)" class=3D"gmail_default">Synt=
ax OK</div></div><div><br></div>-- <br><div dir=3D"ltr" class=3D"gmail_sign=
ature">Fernando Santagata</div></div>

--000000000000b378af05997d7283--
0
nando
12/12/2019 8:22:26 AM

On 2019-12-12 00:22, Fernando Santagata wrote:
> On Thu, Dec 12, 2019 at 3:46 AM Todd Chester <ToddAndMargo@zoho.com=20
> <mailto:ToddAndMargo@zoho.com>> wrote:
>=20
>     On 2019-12-11 10:22, Fernando Santagata wrote:
>      > File test.pm6
>      >
>      > unit module test;
>      > subset PosInt of Int is export where * > 0;
>      >
>      >
>      > File test.p6
>      >
>      > use lib '.';
>      > use test;
>      > sub mytest(PosInt $a) { say $a }
>      > mytest(1);=C2=A0=C2=A0=C2=A0=C2=A0 # output: 1
>      > mytest(-1);=C2=A0=C2=A0=C2=A0 # output Constraint type check faile=
d in binding to
>      > parameter '$a'; expected test::PosInt but got Int (-1)
>=20
>     Hi Fernando,
>=20
>     What response do you get back with?
>=20
>      =C2=A0 =C2=A0 =C2=A0perl6 -c test.pl6
>=20
>     -T
>=20
>=20
> I don't understand the reason for your question, but anyway:
>=20
> $ perl6 -c test.pl6
> Syntax OK
>=20
> --=20
> Fernando Santagata


Hi Frenando,

Thank you!

I was looking to see at what point in the process
the "constraint type" check triggered.

You test showed me that there is no benefit to using
a subset when it comes to trapping the error before
run time.

Another thank you for

     subset PosInt of Int is export where * > 0;

I can export the subset from the module.

Can I export any other variable and constants the same way:

      my $pi =3D 4.13 is export;

-T
0
perl6
12/13/2019 5:44:23 AM
--0000000000006bfd3d0599922ec7
Content-Type: text/plain; charset="UTF-8"

On Fri, Dec 13, 2019 at 6:48 AM Todd Chester via perl6-users <
perl6-users@perl.org> wrote:

> On 2019-12-12 00:22, Fernando Santagata wrote:
> > On Thu, Dec 12, 2019 at 3:46 AM Todd Chester <ToddAndMargo@zoho.com
> > <mailto:ToddAndMargo@zoho.com>> wrote:
>
> Can I export any other variable and constants the same way:
>
>       my $pi = 4.13 is export;
>
> -T
>

Not like that. A "my" variable cannot be exported and the syntax is
slightly wrong.
Try:

our $pi is export = 4.13;

-- 
Fernando Santagata

--0000000000006bfd3d0599922ec7
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail=
_attr">On Fri, Dec 13, 2019 at 6:48 AM Todd Chester via perl6-users &lt;<a =
href=3D"mailto:perl6-users@perl.org">perl6-users@perl.org</a>&gt; wrote:<br=
></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;=
border-left:1px solid rgb(204,204,204);padding-left:1ex">
On 2019-12-12 00:22, Fernando Santagata wrote:<br>
&gt; On Thu, Dec 12, 2019 at 3:46 AM Todd Chester &lt;<a href=3D"mailto:Tod=
dAndMargo@zoho.com" target=3D"_blank">ToddAndMargo@zoho.com</a> <br>
&gt; &lt;mailto:<a href=3D"mailto:ToddAndMargo@zoho.com" target=3D"_blank">=
ToddAndMargo@zoho.com</a>&gt;&gt; wrote:<br><br>
Can I export any other variable and constants the same way:<br>
<br>
=C2=A0 =C2=A0 =C2=A0 my $pi =3D 4.13 is export;<br>
<br>
-T<br>
</blockquote></div><div><br></div><div><div style=3D"color:rgb(0,0,0)" clas=
s=3D"gmail_default">Not like that. A &quot;my&quot; variable cannot be expo=
rted and the syntax is slightly wrong.</div><div style=3D"color:rgb(0,0,0)"=
 class=3D"gmail_default">Try:</div><div style=3D"color:rgb(0,0,0)" class=3D=
"gmail_default"><br></div><div style=3D"color:rgb(0,0,0)" class=3D"gmail_de=
fault">our $pi is export =3D 4.13;<br></div></div><br>-- <br><div dir=3D"lt=
r" class=3D"gmail_signature">Fernando Santagata</div></div>

--0000000000006bfd3d0599922ec7--
0
nando
12/13/2019 9:06:34 AM

On 2019-12-13 01:06, Fernando Santagata wrote:
> our $pi is export = 4.13;


Thank you!
0
perl6
12/13/2019 9:37:29 AM
Reply: