1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Perl 6Advent Calendar 2015

Day 15

Perl6でも0や空文字はFalseだけど

Posted at

こんばんは :whale2:
Perl 6 Advent Calendar 2015の15日目です。(遅刻)

はじめに

use v6;

if !() {
  print "()はFalseです\n";
}

if !'' {
  say "''はFalseです";
}

if !0 {
  put '0はFalseです';
}

しかし

butキーワードを使うことでTrueとして扱うことができます。

use v6;

my $空文字 = '' but role { method Bool  { True } };
my $ゼロ = 0 but True;

say ? $空文字; #=> True
say so $ゼロ; #=> True

でも

配列やハッシュはだめなんだろうか。

use v6;

my @小かっこ = () but True;
my %中カッコ = {} but True;

say @小かっこ.Bool; #=> False
say %中カッコ.so; #=> False

定数constantキーワードを付けるとTrueになるけど…原理がわかったら更新します…1

use v6;

my constant @小かっこ = () but True;
my constant %中カッコ = {} but True;

say @小かっこ.Bool; #=> True
say %中カッコ.so; #=> True

おまけ

逆に、普段はTrueになる値をFalse扱いしたい時にも使えます

use v6;

my Str constant $返事 = 'ぽんぽこタヌキさん' but False ;

$返事.say if $返事.not ; #=> ぽんぽこタヌキさん

おわりです。

参考と注釈

  1. おしえてえらいひと。

1
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?