约束多重及其用于选择它们
Constraining multis and its use for selecting them
约束显然不习惯select一个multi
multi sub cuenta( Str $str ) { say $str };
multi sub cuenta( $file where .IO.e ) { say $file.IO.slurp };
cuenta( $*PROGRAM-NAME ); # Outputs the file name
这意味着它使用的是第一个 multi,而不是第二个。但是,这按预期工作:
subset real-files of Str where .IO.e;
multi sub cuenta( Str $str ) { say $str };
multi sub cuenta( real-files $file ) { say $file.IO.slurp };
cuenta( $*PROGRAM-NAME );
打印程序本身的内容。这可能说明了类型检查和多重调度,但我不确定这是设计使然还是只是一个怪癖。有什么想法吗?
multi sub cuenta( Str $str ) { say $str };
multi sub cuenta( Str $file where .IO.e ) { say $file.IO.slurp };
# ^^^
cuenta( $*PROGRAM-NAME ); # Outputs the file
subset real-files where .IO.e;
# ^^^^^^
multi sub cuenta( Str $str ) { say $str };
multi sub cuenta( real-files $file ) { say $file.IO.slurp };
cuenta( $*PROGRAM-NAME ); # Outputs the file name
首先检查参数的基类型以建立候选者。只有最窄的匹配多重是调度的候选者。 where
约束仅适用于具有相同基类型的多个匹配候选者。如果未指定,则参数或 subset
的基本类型为 Any
.
这是设计使然。
约束显然不习惯select一个multi
multi sub cuenta( Str $str ) { say $str };
multi sub cuenta( $file where .IO.e ) { say $file.IO.slurp };
cuenta( $*PROGRAM-NAME ); # Outputs the file name
这意味着它使用的是第一个 multi,而不是第二个。但是,这按预期工作:
subset real-files of Str where .IO.e;
multi sub cuenta( Str $str ) { say $str };
multi sub cuenta( real-files $file ) { say $file.IO.slurp };
cuenta( $*PROGRAM-NAME );
打印程序本身的内容。这可能说明了类型检查和多重调度,但我不确定这是设计使然还是只是一个怪癖。有什么想法吗?
multi sub cuenta( Str $str ) { say $str };
multi sub cuenta( Str $file where .IO.e ) { say $file.IO.slurp };
# ^^^
cuenta( $*PROGRAM-NAME ); # Outputs the file
subset real-files where .IO.e;
# ^^^^^^
multi sub cuenta( Str $str ) { say $str };
multi sub cuenta( real-files $file ) { say $file.IO.slurp };
cuenta( $*PROGRAM-NAME ); # Outputs the file name
首先检查参数的基类型以建立候选者。只有最窄的匹配多重是调度的候选者。 where
约束仅适用于具有相同基类型的多个匹配候选者。如果未指定,则参数或 subset
的基本类型为 Any
.
这是设计使然。