-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[PPC0022] - naming convention for exceptional vs. undef-returning methods #45
Comments
Aside ThoughtsAs a total aside, I sometimes feel this a far more general and wide-spread naming convention problem across all of Perl, and could be solveable by something like a specific exception type combined with a callsite-modifying keyword. If we had a "Failure" exception, and a method get_symbol($name) {
# some internal code to check if the method exists
... or die Failure("$self does not contain a symbol called '$name'");
return # something about the symbol
}
my $metasym = $metapkg->get_symbol("thing"); # throws if missing
my $metasym = may $metapkg->get_symbol("thing"); # return undef if missing Though one subtlety of this is that you don't want the "Failure" exceptions leaking out to outer sub print_symbol_addr($name) {
my $metasym = $metapkg->get_symbol($name);
printf "Symbol $name is at %x" . $metasym->refaddr;
}
may print_symbol_addr("not-a-symbol"); You don't want the "Failure" to leak out to the outer It's all a messy half-baked thought I haven't really finished thinking about yet... but in any case it shouldn't hold up the design of |
I was just thinking, it is much simpler and less mistake-prone to append |
Mmmm.. It can be in some cases, but I don't like it as a general solution because it has real DRY-failures at times. Consider if my $metasym = getsym('$'.join("::", $self->{caller_package}, $self->symname_for("thing")))
// die "Cannot obtain symbol named ... err... do I have to repeat the entire expression above all over again?"; The great thing about having the invoked function yield its own failure name is that it lets you generate that human-readable message a lot simpler. |
To explain more succinctly: Turning an entire class of exception into an undef (i.e. with the |
A cooll thing we could do building on this idea (which i'm all in on) is that we can detect if a sub could fail, and then have a strict pragma that forces you to handle failures at the callsite. that would be pretty awesome |
I'm currently converting some no strict 'refs';
push @{"${caller}::ISA"}, __PACKAGE__; The new code currently looks like: my $callermeta = meta::package->get( $caller );
push @{ $callermeta->get_symbol( '@ISA' )->reference }, __PACKAGE__; Except this fails with:
I could write this as push @{ ($callermeta->try_get_symbol( '@ISA' ) // $callermeta->add_symbol('@ISA'))->reference }, __PACKAGE__; but clearly that sucks for length, DRY, readability and overall sanity. We can do better. I feel now that we need three variants of the get-style methods, which all differ in what they do if the requested thing didn't exist:
Of course, the latter case isn't always appropriate. You can create a missing package, glob, scalar/array/hash variable if it didn't exist, but you can't create a missing subroutine. So this variant wouldn't always exist. Alternative ideas: Create a little set of (exported?) flag constants and supply an optional second argument to the get method: get($name); # or throw
get($name, META_MISSING_OK); # or return undef
get($name, META_CREATE); # or create Ugly As Sin though, and it gets in the way of the idea that maybe my $metaglob = meta::glob->get($pkgname, "DATA"); # obtain the package's \*DATA glob |
The original PPC doc suggested two sets of "fetch an element from a meta-package" methods, named "get_..." and "can_..." to reflect the difference between methods that throw exceptions and methods that return
undef
when the requested entity does not exist.My original inspiration for
can_...
came from Perl's own$pkg->can(...)
which returns a coderef or undef. But perhaps it's not so great.In addition, the API shape suggested by #44 leads to an alternative form of fetching metasymbols directly, by doing things like
Under that style, using
->can
would not work. Perahps instead take inspiration from https://metacpan.org/pod/Object::Pad::MOP::Class#try_for_class and useget_...
vstry_get_...
That also works for the constructor-style ones:
The text was updated successfully, but these errors were encountered: