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

[email protected] (Elizabeth Mattijsen) Mon, 20 Oct 2025 17:19:13 +0200
Newsgroups perl.perl6.users
Message-ID <[email protected]>
> On 20 Oct 2025, at 16:59, Quanrong via perl6-users <[email protected]> wrote:
> Hi all, I'm learning Raku and enjoying it tremendously, it's a beautiful and well thought language.

Glad to hear that!


> I was very surprised to find that, even if you declare the return type of a subroutine with the `:D` smiley, subroutines are still allowed to return `Nil`.
> 
> What is the reason for this? It would be very useful to be able to state that a subroutine must not return `Nil`. And, if this is not possible, shouldn't the `:D` smiley be disallowed for the return type in a signature?
> 
> I suppose there has to be a good reason for this.

It was designed that way.   This also goes for Failure objects, which are a subclass of Nil.

Nil (and its subclass Failure) are exempt from type checking.

The case of Failure should be clear: if you want to return a Failure because something went wrong, you don't want it to trip off the type checking.

You should think of Nil as a type of benevolent Failure: nothing really wrong, but it could not produce an actual value.


Hope this made sense!