Must specify a non-empty string as a path
I always try a person's code when looking at someone's SO. Yours didn't work. (No declaration of $vwf
.) That instantly alerts me that someone hasn't applied Minimal Reproducible Example principles.
So I did and less than 60 seconds later:
IO::Path.new
Yields the same error.
Why?
The doc for IO::Path.new
shows its signature:
multi method new(Str:D $path, ...
So, IO::Path
's new
method expects a positional argument that's a Str
. You (and my MRE) haven't passed a positional argument that's a Str
. Thus the error message.
Of course, you've declared your own attribute $path
, and have passed a named argument to set it, and that's unfortunately confused you because of the coincidence with the name path
, but that's the fun of programming.
What next, take #1
Having a path attribute that duplicates IO::Path
's strikes me as likely to lead to unnecessary complexity and/or bugs. So I think I'd nix that.
If all you're trying to do is wrap an additional check around the filename, then you could just write:
unit class Vimwiki::File is IO::Path;
method new ($path, |) { $path.IO.e ?? (callsame) !! die 'nope' }
callsame
redispatches the ongoing routine call (the new
method call), with the exact same arguments, to the next best fitting candidate(s) that would have been chosen if your new one containing the callsame
hadn't been called. In this case, the next candidate(s) will be the existing new
method(s) of IO::Path
.
That seems fine to get started. Then you can add other attributes and methods as you see fit...
What next, take #2
...except for the IO::Path
bug you filed, which means you can't initialize attributes in the normal way because IO::Path
breaks the standard object construction protocol! :(
Liz shows one way to workaround this bug.
In an earlier version of this answer, I had not only showed but recommended another approach, namely delegation via handles
instead of ordinary inheritance. I have since concluded that that was over-complicating things, and so removed it from this answer. And then I read your issue!
So I guess the delegation approach might still be appropriate as a workaround for a bug. So if later readers want to see it in action, follow @sdondley's link to their code. But I'm leaving it out of this (hopefully final! famous last words...) version of this answer in the hope that by the time you (later reader) read this, you just need to do something really simple like take #1.