Created
September 22, 2023 04:48
-
-
Save gugod/633bb6bcbf4b9fb43e01a1fb27719ce0 to your computer and use it in GitHub Desktop.
Nay -- an object of negative identity.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Nay; | |
use v5.36; | |
no warnings 'experimental::builtin'; | |
use builtin 'false'; | |
use overload 'bool' => sub { false }; | |
use Exporter 'import'; | |
our @EXPORT = qw(Nay); | |
my $Nay = bless {}, 'Nay'; | |
sub Nay() { $Nay } | |
sub AUTOLOAD { $Nay } | |
1; | |
=head1 Nay | |
use Nay; | |
package Foo { | |
sub bar { rand() > 0.5 ? $_[0] : undef } | |
sub buz { "buz" } | |
}; | |
my $foo = bless {}, 'Foo'; | |
# Append '// Nay' after calling methods that might return undef. | |
# All following method calls may be reduced to Nay. | |
my $buz = ($foo->bar() // Nay) -> buz(); | |
if ($buz) { | |
say "buz() is called"; | |
} else { | |
say "Nay."; | |
} | |
=cut |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment