Replies: 5 comments 3 replies
-
Furthermore I don't really ever think I'd use At which point if there's only |
Beta Was this translation helpful? Give feedback.
-
I think I agree with this. Caring about the distinction between
I'm not saying they're bad, per se, but we've worked so hard to prevent illegal combination that maybe a we can find a way to continue that? What about this? field $foo :param(?) = 'default'; # allow undef to be passed and not use the default
field $foo :param(is_foo?) = 'default'; # same thing, but allow is_foo to be the constructor parameter name Not convinced by my syntax, either, because it doesn't seem intuitive. |
Beta Was this translation helpful? Give feedback.
-
copying here what I wrote on fedi:
for subroutine signatures, for consistency, I say:
|
Beta Was this translation helpful? Give feedback.
-
I can't say whether I would miss it given that attributes are already very different than a hash-based object, but I use the existence of attributes that can be undef quite often as a measure of whether a value has been cached. |
Beta Was this translation helpful? Give feedback.
-
After much deliberation, thinking, discussion on IRC, Mastodon, and so on, I have come to the conclusion that "Yes, we have the wrong behaviour by default", but also that we can't change it. It's somewhat annoying that sub f($x, $y = "default-if-missing", $z //= "default-if-undef") { ... }
ADJUST :params (
:$x, :$y = "default-if-missing", :$z //= "default-if-undef"
) { ... }
# therefore
field $x :param;
field $y :param = "default-if-missing";
field $z :param //= "default-if-undef"; It is perhaps regrettable that subroutine signatures took that behaviour initially, rather than finding some other notation. But we seem to be stuck with it now. In an alternate universe, Perl would have added a "hash element exists or" operator (perhaps spelled like ilmari's |
Beta Was this translation helpful? Give feedback.
-
The more I use this, the more I feel that we got the defaults wrong with
field :param
with a defaulting expression.The current behaviour says that a field with
:param
and a defaulting expression attached with the=
operator, will only apply the default if the caller did not even pass in a named parameter to the constructor at all. Any value at all - even undef - will override this default. We added the notion that fields can be written//=
to express that they want the default to also replace an undef.But basically every single time I add a
field $name :param
with a default, I end up wanting to put the//= default
on it. I basically never care about undef being a thing. undef should behave like missing. Across 12 years, over two hundred CPAN modules and well over a thousand .pm files, I think I could count maybe two or three times I have cared about the distinction between passing undef and not passing anything. It feels like every single time I write//=
I begin to wish that it should have been the normal behaviour for just writing=
and in the rare case you actually did care to distinguish undef from missing, you'd want to draw special attention to it.So maybe in that rare case you'd have to write something like one of these:
I dunno. I don't really like those spellings. We should find something better. But overall, I think we might want to consider making the "normal" thing of
apply the default on both missing and present-but-undefined values being passed to the constructor, and require the class author to explicitly request differently if that is what they really mean. I think it would avoid surprises on both sides.
Beta Was this translation helpful? Give feedback.
All reactions