Replies: 6 comments 16 replies
-
On the necessity of
|
Beta Was this translation helpful? Give feedback.
-
I am not concerned about In #77 we discuss several examples where individual constructor parameters don't map directly to fields, and I'm wondering how the Taking a step back: I guess that the "default-if-undef" situation is more frequent than nontrivial parameter mappings, and therefore more relevant for a replacement by a declarative syntax. We could declare the stuff from #77 as "too ambitious" for now. Nontrivial mappings between constructor parameters and fields can still be done in |
Beta Was this translation helpful? Give feedback.
-
class C {
my $one = say "This is printed once";
field $two = say "This is printed three times";
}
say "This happens inbetween";
C->new for 1 .. 3; I would argue that we already have this distinction: sub C {
my $one = say "This is printed three times";
state $two = say "This is printed once";
}
say "This happens before";
C() for 1..3; Admittedly, the timing of things are a touch different, but when someone sees, for example, the various attributes attached to a So I suspect that this will largely be a matter of educating people on OO and reminding them that a top-level |
Beta Was this translation helpful? Give feedback.
-
Short answer: Yes, we should use Long answer: Yes, we should definitely use The fact that:
...executes the
...especially that last case. The loss of:
...is (as @HaraldJoerg) notes) a minor one, since we still have:
That’s an acceptable pessimization in my view, given the far greater need for consistent and flexible default initializations, (Though, of course, the
;-) |
Beta Was this translation helpful? Give feedback.
-
Well...after a day of soul-searching, a good night’s sleep, My most recent proposal was a complex three-part Now that the Benedryl has worn off, I have come to feel that such a design Because the various proposals we’re now discussing So what is the minimal useful behaviour?
And that’s it. With those two, we get all of the following: # Uninitialized field (i.e. value is undef)...
field $colour;
# Field initialized from 'colour' NCA...
field $colour :param;
# Field initialized from 'color' NCA...
field $colour :param(color);
# Field initialized with constant...
field $colour = Colour->new(r=>0, g=>0, b=>0);
# Field initialized from another field...
field $background = $colour->complement();
# Field initialized from a method of the object...
field $background = $self->get_colour->complement();
# Field initialized from a complex block of code...
field $background = do { my $bg = Colour->random_colour();
while (contrast($colour, $bg) < 0.25) {
$bg = $bg->offset(('r','g','b')[rand 3] => rand(256));
}
$bg;
};
# Field initialized from NCA or from an expression if the NCA is missing...
field $background :param(bg) = $colour->complement(); ...which is surely enough for an initial minimal feature set. For the time being, any more complex initialization requirements should be handled I guess I don’t really mind if the But that’s as far down the slippery slope of featuritis as I think we should I’m sorry that I forgot that mandate myself, and more especially |
Beta Was this translation helpful? Give feedback.
-
I know we had justabout settled on field initialisation expressions appearing in braces, but a troubling thought occurred to me today.
On Defaulting Expressions in Signatures
First, a bit of background about function signatures. Prior to using signatures, it would be common to see code such as:
This requires a first parameter to assign to
$x
, and optionally a second for$y
, but if the caller did not pass a defined value (i.e. they passed undef, or they passed nothing at all) then the defaulting expression runs instead. This distinction didn't make it into the first version of signatures. The way that signatures currently work makes the following two roughly equivalent:Because it turns out in practice that actually a lot of the time people care about definedness and not mere existence, there is a proposal to add syntax using
//=
instead. Thus, the following would be more equivalent to the first function above:My recent addition of named parameter support to
ADJUST
blocks in Object::Pad has already hit upon this annoying distinction. I did want to provide default values using this kind of syntax, but again it turns out that the simple implementation cares about existence rather than definedness. In particular, in this code:https://metacpan.org/dist/Device-Serial-SLuRM/source/lib/Device/Serial/SLuRM.pm#L249
Somewhat paraphrased:
This is pretty icky, and all because elsewhere in the container script I use Getopt::Long to handle commandline arguments:
Were instead we to have a similar syntax for
ADJUST :params
that cares about definedness then this entire problem goes away. Most likely that would also be spelled with//=
.On Field Expressions
So what does this have to do with fields? Well, it becomes a problem when we want to run a field defaulting expression whenever the parameter was not defined (i.e. not present, or present as the undefined value). Right now we cannot; having instead to resort to plain ADJUST blocks which look a bit messy and disconnected in the same way the
$baud
variable is above.This feels a bit awkward and circumspect. If the syntax for attaching defaulting expressions on fields used
=
then we would have a convenient place to put a//=
operator instead. This would have the huge advantage of preserving Perl's "similar things look similar" behaviour, which I think is important.However, this does have to be weighed against the problem that now
field
andmy
behave very differently in what look like similar circumstances. Try explaining to someone why the output of this is what it is:generates:
However - overall I am beginning to prefer
=
and//=
as an idea, over{...}
. Using=
means that//=
is even possible even if we have to explain thatfield
andmy
happen at different times; whereas if we stick to{...}
then I don't think a syntax for//=
-like behaviour is even very possible. If it is it would look quite different to function parameters and ADJUST blocks.Can anyone see a way out of this one?
Beta Was this translation helpful? Give feedback.
All reactions