This code (for checking the changed timestamp of the current directory):
my $date = ".".IO.changed.DateTime.yyyy-mm-dd but Dateish;
say $date;
yields the error:
«Ambiguous call to 'gist(Str+{Dateish}: )'; these signatures all match:: (Str:D: *%_):(Dateish:D: │ avalenn
| *%_) in block <unit> at <tmp> line 1»
Without the Dateish
mix in, the string is simply 2018-05-12
. Using any other kind of Dateish
function, like .week-year
also yields a different error:
«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»
Does it simply mean that you can't mix in a string with Dateish? I've done something similar with hours without a problem.
To answer your question, we should look at that role:
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 }
...
}
So, role Dateish
has several attributes, and the methods use those attributes to calculate their return values.
When you do $some-string but Dateish
, you are not doing anything to initialize the attributes, and thus method calls that use them (potentially indirectly) fail in interesting ways.
How do you get a Dateish
object from a DateTime
then? Well, DateTime
is one, already, or you can coerce to Date
if that is what you want:
my $date = ".".IO.changed.DateTime.Date; say $date
You might also try to instantiate a Dateish
by supplying all attributes, but I don't see any advantage over just using Date
as intended.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With