Re: Rationale of return types from subroutines always being nullable?

[email protected] (Elizabeth Mattijsen) Tue, 21 Oct 2025 10:55:24 +0200
Newsgroups perl.perl6.users
Message-ID <[email protected]>

> On 21 Oct 2025, at 09:23, ToddAndMargo via perl6-users <[email protected]> wrote:
> 
> On 10/20/25 10:28 AM, Quanrong via perl6-users wrote:
>> > Oh, but it *does* serve a purpose, and it *will* fire under the right circumstances!  For instance:
>> >
>> > $ raku -e 'sub a(--> Int:D) { Int }; a'
>> > Type check failed for return value; expected Int:D but got Int (Int)
>> >    in sub a at -e line 1
>> >    in block <unit> at -e line 1
>> You are right! I had missed that, it's very obvious in retrospective. Now I understand.
>> Thanks for your help! And for working on Raku :)
> 
> 
> Okay, made me look up what an Int:D is.  This back
> from search.brave.com's AI:
> 
>    In Raku, Int:D refers to a type constraint that specifies
>    a defined value of the Int type. It is used to restrict
>    a variable or parameter to only accept instances of the
>    Int class (i.e., actual integer values), excluding the
>    Int type object itself. For example, declaring
>      my Int:D $x = 42;
>    ensures that $x can only hold an integer value and not
>    the Int type object. This constraint is useful for enforcing
>    that a variable contains a concrete, defined integer rather
>    than a type object. The :D smiley stands for "definite,"
>    indicating that the value must be defined.
> 
> I use Int a lot.  When would Int:D be useful?  When you
> want the finger wagged at you if you send a Nil?
> 
> Yours in confusion,

Well, you could want it when you want to make sure something gets initialized:

$ raku -e 'sub a(--> Int:D) { my Int $b; $b }; a'
Type check failed for return value; expected Int:D but got Int (Int)
  in sub a at -e line 1
  in block <unit> at -e line 1