raku 中的冒号

Raku Colons

在 Raku 中到处都是冒号, 我搜集了你在 Raku 中使用冒号的所有方式。

Larry 语言再设计第一定律: 每个人都想要冒号
Larry 语言再设计第二定律: Larry 成为冒号

这些都是主题的变体,尤其是“冒号对”语法主题。如果冒号的两种用法看起来不同,或者在不同的用法上下文中不同,即使它们在技术上是相同的,我更愿意说明这两种用法。

命名空间

# 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;

绑定

# Binding
my $y := $a;

# Read-only binding
my $z ::= $y;

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

Colon-Pair 语法

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

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

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

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

# Default to true value
say (:foo)     # 'foo' => True
:foo(True)     # 'foo' => True

# 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

类型

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

# Type Smileys
Int:D; # Defined Int
Int:U; # Undefined Int

Signature

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

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

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)

Regex

# Regex adverb
:i/a/  # Case insensitive, outside
/:i a/ # Case insensitive, inside

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

# Backtracking control
# similar ones not yet implemented are :: ::> :::
/ a: b?: c*: /

# One especially interesting example you often (don't) see is :?
/ .*:? / # explicit minimal match
/ .*? /  # short version that is usually used has no colon

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

Misc

# Quote adverb
Q:w(word of the day); # :w adverb

# 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

# Declare alternative to 'self' for current-instance in method
method wuzzup($this: $name) { say "WUZZUP $name?!" }

# object hashes, ones with complex keys
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

# This one is a stretch...
# Colons instead of dashes when you invoke cli commands that use MAIN
sub MAIN ( :$filename ) { ... }

# cmd --filename=file.txt
# cmd :filename=file.txt
# cmd :filename # filename => true

冒号真是令人惊叹! Awesome.