Perl 6 中的冒號

Perl 6 Colons

在 Perl 6 中到處都是冒號, 我搜集了你在 Perl 6 中使用冒號的所有方式:

命名空間

# namespace
package A::B { ... }
class   A::B { ... }

# Namespace separator
my $x = A::B.new;

# Dynamic namespace
my $x = ::("A::B").new;

# Pseudopackage representing null namespace
say ::;

# Anonymous class
class :: is Int {...}

# Current class in a compile-tiem var
class Who { method myname { say ::?CLASS } }

# Call Int method of Int class
42.Int::Int; # => 42

# Treat package X as a hash
say X::.keys;

類型

# T takes value's type
-> Numeric ::T \x { say T }(42); # => (Int)

# Type abverb (smiley)
Int:D;

綁定

# Binding
my $y := $a;

# Container identify
$y =:= $a; # True

# Compile-time (read-only) binding
my $z ::= $y;

Signature

# signature literal
my $sig = :(Int $foo);

# signature on Callable &var
my &f:(Str) = -> Str {};

Colon-Pair 語法

# Colon-Pair syntax (often seen as adverbs)
my $x = 'cat';
:foo           # 'foo' => True
:foo(True)     # 'foo' => True
:foo(5)        # 'foo' => 5
:foo('dog')    # 'foo' => 'dog'
:foo($x)       # 'foo' => 'cat'

# Value quoting
:foo<bar>      # 'foo' => 'bar'
:foo<bar baz>  # 'foo' => ('bar', 'baz')
:foo<$x>       # 'foo' => '$x'

# value quoting with interpolation
:foo<<$x>>     # 'foo' => 'cat'
:foo<<$x dog>> # 'foo' => ('cat', 'dog')

# Numeric-prefix extraction
say (:73day) # day => 73

# False instead of true
say (:!foo) # foo => False

# Var name as key, content as value
my @foo=1,2;
:@foo # foo => [1,2]
:@foo # foo => [1,2]
:$x   # x   => 'cat'

# Base conversion via colon-pair
say :16("dead") # 57005

# binding pair in sig:
my $a;
sub b (:foo($a)! is rw) { $a = 42};
b(:foo($a));
say $a # 42

Regex

# Declare vars in regex scope
my regex foo { :my $var; }

# FUTURE -- http://design.perl6.org/S05.html#Backtracking_control (some of these implemented already, Larry says he plans to do more this year)
my regex bar { a: b:? c:! :: ::> }

# Unicode character classes
say so "a" ~~ /<[:Alpha]>/

Operators as Methods

my $a = 1;
$a.infix:["+"](3); # $a + 3
$a.infix:<+>(3);   # $a + 3, but shorter with quote brackets
$a.prefix:<++>;    # ++$a
$a.postfix:<++>;   # ++$a
$a.:<++>;          # Shorthand for prefix (no param)
$a.:<+>(3);        # Shorthand for infix (param provided)

Misc

# Precedence dropper
@stuff.map({ $_ + 1 }); # Explicit parameter
@stuff.map: { $_ + 1 }; # Parameters come after ':'

# Invocant marker method invocation
$x.say-hi(names => [<me you>]);  # Normal way
say-hi($x: names => [<me you>]); # Invocant marker way

# object hashes
my $x = :{ (now) => "when?" };

# longname
sub infix:<ya> { ... }

# Specific uses of longnames
use Foo:from<Perl5>;

# Loop labels
MYLABEL: for ^100 {
  for ^200 {
    last MYLABEL;
  }
}

# Twigil for formal named param for a block (like $^x)
say { $:add ?? $^a + $^b !! $^a - $^b }( 4, 5 ) :!add

評論

下面這兩句是等價的。

class TreeNode { has TreeNode @children; }
class TreeNode { has ::?CLASS @children; }

冒號真是令人驚嘆! Awesome.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 從匹配中返回值 Match 對象 成功的匹配總是返回一個 Match 對象, 這個對象通常也被放進 $/ 中, (...
    焉知非魚閱讀 1,937評論 0 1
  • 捕獲 簽名不僅僅是語法,它們是含有一列參數(shù)對象的 first-class 對象 。同樣地,有一種含有參數(shù)集的數(shù)據(jù)...
    焉知非魚閱讀 698評論 0 0
  • 2009 有用的和有意思的循環(huán) 讓我們來看一個基本的例子. 這是一個最簡單清晰的語法的例子.在這并沒有使用括號來包...
    焉知非魚閱讀 652評論 0 0
  • 允許的修飾符 有些修飾符能在所有允許的地方出現(xiàn), 但并非所有的都這樣. 通常, 影響 regex 編譯的修飾符(...
    焉知非魚閱讀 1,591評論 0 1
  • Set Series Operator flip_flop.txt 內(nèi)容如下: 輸出: Grammars 只能在葉...
    焉知非魚閱讀 960評論 0 0

友情鏈接更多精彩內(nèi)容