函数::参数 - “在方法中......:'['之后缺少类型名称”是什么意思

Ed.*_*Ed. 2 perl

我在使用模块Function::Parameters时遇到问题Types::Standard。在此代码中,在 Person.pm 中:

package Person;

use strict;
use warnings;
use Function::Parameters;
use Types::Standard qw(InstanceOf);

method is_taller_than(
  InstanceOf['Person'] $other
) {
  return;
}

1;
Run Code Online (Sandbox Code Playgroud)

使用perl -cw Person.pm报告:

In method is_taller_than: missing type name after '[' at Person.pm line 9.
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?

Ed.*_*Ed. 7

这看起来像是Function::Parameters. 它可以处理裸字和方括号,但不能处理带引号的字符串。解决这个问题的方法就是在类型表达式周围加上括号:

method is_taller_than(
  (InstanceOf['Person']) $other
) {
Run Code Online (Sandbox Code Playgroud)

  • 另一个解决方法:`使用常量 { InstanceOfPerson => InstanceOf['Person'] }; 方法 is_taller_than(InstanceOfPerson $other) { ... }`。甚至`use constant { Person => 'Person' }; 方法 is_taller_than(InstanceOf[Person] $other) { ... }`。 (5认同)