Re: Checking for nil return

[email protected] (yary) Mon, 28 Dec 2020 16:35:49 -0500
Newsgroups perl.perl6.language
Message-ID <CAG2CFAbE6OyjOo=5euN26tgmPeHpGMQf=imu3UvHpPz7ehAwzQ@mail.gmail.com>
--0000000000006363ba05b78d0f90
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Been thinking about this, and checked out the Rakudo repository to peek
into the source.

Allowing Failure as a return always makes sense to me=E2=80=93 every block =
needs to
be capable of passing along a failure, that's how the language is designed.

On the other hand, Nil is not a Failure. Conceptually it is a lack of an
answer, similar to SQL's null concept.

What's the usefulness of having Nil skip return type checking- specifically
Nil and not its Failure descendents?

This example under https://docs.raku.org/type/Nil shows what I think is a
less-than-awesome specification, and I am curious about the reasoning
behind it being defined as valid

sub a( --> Int:D ) { return Nil }



-y


On Sun, Dec 20, 2020 at 7:18 PM Brad Gilbert <[email protected]> wrote:

> Nil is always a valid return value regardless of any check.
>
> This is because it is the base of all failures.
>
> On Sat, Dec 19, 2020, 8:17 PM yary <[email protected]> wrote:
>
>> Is this a known issue, or my misunderstanding?
>>
>> > subset non-Nil where * !=3D=3D=3D Nil;
>> (non-Nil)
>> > sub out-check($out) returns non-Nil { return $out }
>> &out-check
>> > out-check(44)
>> 44
>> > out-check(Nil)
>> Nil
>>
>> ^ Huh, I expected an exception on "out-check(Nil)" saying the return
>> value failed the "returns" constraint.
>>
>> The subtype works as I expect as an the argument check
>>
>> > sub in-check (non-Nil $in) { $in }
>> &in-check
>> > in-check(33)
>> 33
>> > in-check(Nil)
>> Constraint type check failed in binding to parameter '$in'; expected
>> non-Nil but got Nil (Nil)
>>   in sub in-check at <unknown file> line 1
>>   in block <unit> at <unknown file> line 1
>>
>> $ raku --version
>> This is Rakudo version 2020.07 built on MoarVM version 2020.07
>> implementing Raku 6.d.
>>
>> Is this my understanding of return type checking that's off, or a known
>> issue, or something I should add to an issue tracker?
>>
>> -y
>>
>

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

<div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr">Been thinking about this=
, and checked out the Rakudo repository to peek into the source.<div><br></=
div><div>Allowing Failure as a return always makes sense to me=E2=80=93 eve=
ry block needs to be capable of passing along a failure, that&#39;s how the=
 language is designed.</div><div><br></div><div>On the other hand, Nil is n=
ot a Failure. Conceptually it is a lack of an answer, similar to SQL&#39;s =
null concept.</div><div><br></div><div>What&#39;s the usefulness of having =
Nil skip return type checking- specifically Nil and not its Failure descend=
ents?</div><div><br></div><div>This example under <a href=3D"https://docs.r=
aku.org/type/Nil">https://docs.raku.org/type/Nil</a> shows what I think is =
a less-than-awesome specification, and I am curious about the reasoning beh=
ind it being defined as valid</div><div><br></div><div><blockquote class=3D=
"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left-width:1px;borde=
r-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><sp=
an class=3D"gmail-storage gmail-type gmail-declarator gmail-type gmail-raku=
" style=3D"color:rgb(166,38,164);font-weight:bold;font-family:monospace;fon=
t-size:14px;white-space:pre-wrap">sub</span><span style=3D"color:rgb(0,0,0)=
;font-family:monospace;font-size:14px;white-space:pre-wrap">=C2=A0</span><s=
pan class=3D"entity gmail-name gmail-function gmail-raku" style=3D"color:rg=
b(153,0,0);font-family:monospace;font-size:14px;white-space:pre-wrap">a</sp=
an><span style=3D"color:rgb(0,0,0);font-family:monospace;font-size:14px;whi=
te-space:pre-wrap">(=C2=A0</span><span class=3D"gmail-keyword gmail-operato=
r gmail-multi-symbol gmail-raku" style=3D"font-weight:bold;font-family:mono=
space;font-size:14px;white-space:pre-wrap">--&gt;</span><span style=3D"colo=
r:rgb(0,0,0);font-family:monospace;font-size:14px;white-space:pre-wrap">=C2=
=A0</span><span class=3D"gmail-support gmail-type gmail-raku" style=3D"colo=
r:rgb(68,85,136);font-family:monospace;font-size:14px;white-space:pre-wrap"=
>Int</span><span class=3D"gmail-meta gmail-adverb gmail-definedness gmail-r=
aku" style=3D"color:rgb(0,0,0);font-family:monospace;font-size:14px;white-s=
pace:pre-wrap"><span class=3D"gmail-keyword gmail-operator gmail-adverb gma=
il-raku" style=3D"color:rgb(34,34,34);font-weight:bold">:</span><span class=
=3D"gmail-keyword gmail-other gmail-special-method gmail-definedness gmail-=
raku" style=3D"color:rgb(0,86,195);font-weight:bold">D</span></span><span s=
tyle=3D"color:rgb(0,0,0);font-family:monospace;font-size:14px;white-space:p=
re-wrap">=C2=A0)=C2=A0</span><span class=3D"gmail-meta gmail-block gmail-ra=
ku" style=3D"color:rgb(0,0,0);font-family:monospace;font-size:14px;white-sp=
ace:pre-wrap"><span class=3D"gmail-punctuation gmail-definition gmail-block=
 gmail-raku">{</span>=C2=A0<span class=3D"gmail-keyword gmail-control gmail=
-flowcontrol gmail-raku" style=3D"color:rgb(34,34,34);font-weight:bold">ret=
urn</span>=C2=A0<span class=3D"gmail-support gmail-type gmail-raku" style=
=3D"color:rgb(68,85,136)">Nil</span>=C2=A0<span class=3D"gmail-punctuation =
gmail-definition gmail-block gmail-raku">}</span></span></blockquote><div><=
br></div><div><br></div><div><div dir=3D"ltr" class=3D"gmail_signature">-y<=
br></div></div><br></div></div></div></div><br><div class=3D"gmail_quote"><=
div dir=3D"ltr" class=3D"gmail_attr">On Sun, Dec 20, 2020 at 7:18 PM Brad G=
ilbert &lt;<a href=3D"mailto:[email protected]">[email protected]</a>&gt; w=
rote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0p=
x 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb=
(204,204,204);padding-left:1ex"><div dir=3D"auto">Nil is always a valid ret=
urn value regardless of any check.<div dir=3D"auto"><br></div><div dir=3D"a=
uto">This is because it is the base of all failures.</div></div><br><div cl=
ass=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Sat, Dec 19, 2=
020, 8:17 PM yary &lt;<a href=3D"mailto:[email protected]" target=3D"_blank=
">[email protected]</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quot=
e" style=3D"margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-styl=
e:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir=3D"lt=
r"><div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div=
 dir=3D"ltr"><div dir=3D"ltr">Is this a known issue, or my misunderstanding=
?</div><div dir=3D"ltr"><br></div><div dir=3D"ltr">&gt; subset non-Nil wher=
e * !=3D=3D=3D Nil;<br></div><div dir=3D"ltr">(non-Nil)</div><div dir=3D"lt=
r"><div dir=3D"ltr"><div dir=3D"ltr">&gt; sub out-check($out) returns non-N=
il { return $out }</div><div dir=3D"ltr">&amp;out-check</div><div dir=3D"lt=
r">&gt; out-check(44)</div><div dir=3D"ltr">44</div><div dir=3D"ltr">&gt; o=
ut-check(Nil)</div><div dir=3D"ltr">Nil</div><br>^ Huh, I expected an excep=
tion on &quot;out-check(Nil)&quot; saying the return value failed the &quot=
;returns&quot; constraint.</div><div dir=3D"ltr"><br></div><div>The subtype=
 works as I expect as an the argument check</div><div dir=3D"ltr"><br></div=
><div dir=3D"ltr">&gt; sub in-check (non-Nil $in) { $in }</div><div dir=3D"=
ltr">&amp;in-check</div><div dir=3D"ltr">&gt; in-check(33)</div><div dir=3D=
"ltr">33</div><div dir=3D"ltr">&gt; in-check(Nil)</div><div dir=3D"ltr">Con=
straint type check failed in binding to parameter &#39;$in&#39;; expected n=
on-Nil but got Nil (Nil)</div><div dir=3D"ltr">=C2=A0 in sub in-check at &l=
t;unknown file&gt; line 1</div><div dir=3D"ltr">=C2=A0 in block &lt;unit&gt=
; at &lt;unknown file&gt; line 1</div><div><br></div></div><div dir=3D"ltr"=
><div dir=3D"ltr">$ raku --version</div><div dir=3D"ltr">This is Rakudo ver=
sion 2020.07 built on MoarVM version 2020.07</div><div dir=3D"ltr">implemen=
ting Raku 6.d.</div></div><div dir=3D"ltr"><br></div><div>Is this my unders=
tanding=C2=A0of return type checking that&#39;s off, or a known issue, or s=
omething I should add to an issue tracker?</div><div><br></div><div dir=3D"=
ltr"><div><div dir=3D"ltr">-y<br></div></div></div></div></div></div></div>=
</div></div>
</blockquote></div>
</blockquote></div>

--0000000000006363ba05b78d0f90--