Dateish字符串的问题

jjm*_*elo 6 date mixins perl6

此代码(用于检查当前目录的更改时间戳):

my $date = ".".IO.changed.DateTime.yyyy-mm-dd but Dateish; 
say $date;
Run Code Online (Sandbox Code Playgroud)

产生错误:

«Ambiguous call to 'gist(Str+{Dateish}: )'; these signatures all match:?:  (Str:D: *%_)?:(Dateish:D:   ? avalenn
                 | *%_)?  in block <unit> at <tmp> line 1??»
Run Code Online (Sandbox Code Playgroud)

没有Dateish混合,字符串就是简单的2018-05-12.使用任何其他类型的Dateish函数,.week-year也会产生不同的错误:

«Str+{Dateish}?Invocant of method 'Bridge' must be an object instance of type 'Int', not a type      ? a3r0
                 | object of type 'Int'.  Did you forget a '.new'??  in block <unit> at <tmp> line 1??»
Run Code Online (Sandbox Code Playgroud)

它只是意味着你不能将一个字符串与Dateish混合在一起吗?我做了几个小时没有问题的事情.

mor*_*itz 6

要回答你的问题,我们应该看一下这个角色:

my role Dateish {
    has Int $.year;
    has Int $.month;     # should be int
    has Int $.day;       # should be int
    has Int $.daycount;
    has     &.formatter;

    ...
    multi method Str(Dateish:D:) {
        &!formatter ?? &!formatter(self) !! self!formatter
    }
    multi method gist(Dateish:D:) { self.Str }
    ...
}
Run Code Online (Sandbox Code Playgroud)

因此,role Dateish有几个属性,方法使用这些属性来计算它们的返回值.

当你这样做时$some-string but Dateish,你没有做任何事情来初始化属性,因此使用它们的方法调用(可能是间接的)以有趣的方式失败.

你怎么从那时得到一个Dateish对象DateTime?嗯,DateTime已经是一个,或者你可以强迫,Date如果这是你想要的:

my $date = ".".IO.changed.DateTime.Date; say $date
Run Code Online (Sandbox Code Playgroud)

您也可以尝试Dateish通过提供所有属性来实例化a ,但是我没有看到任何优势而不仅仅是Date按预期使用.