My own thoughts on what are good design...
For actual mutable objects such as these are, the writer methods should return
void, as they are statements and not expressions.
Were we talking about immutable objects such that a "setter" is actually a
shorthand for creating a new object with the same fields as the invocant but for
one field value being changed, then it should return the new object. But this
doesn't apply here.
Personally I'm a strong advocate for immutable objects where one sets all the
fields at initial construction time, and not using chained mutators.
I'm not going to argue strongly against a chained mutator design, but that's my
preference to not encourage it.
Another thing to consider though is if you want to be conservative then making
mutators void in the initial version is safest because you're providing the
simplest functionality one can rely on. You can choose to later have them
return the invocant to support chaining mutators, but you can't change chaining
to void later without breaking things.
-- Darren Duncan
On 2024-02-14 1:42 a.m., Ruud H.G. van Tol via perl5-porters wrote:
>
> On 2024-02-12 17:45, Ovid wrote:
>> [...]
>> The only caveat is I realize the team never came to an agreement about what
>> the :writer would return. Some insist upon the invocant (chaining mutators)
>> while others want void. Returning a true value or previous value were minority
>> opinions.
>>
>
> Some general remarks:
>
> Chaining is mostly only good for configuration and setup contexts.
>
> You generally start out with a copy of a default configuration, as an object,
> and then specialize it.
> Once specialized enough, you call something like $task->run, to put the
> configuration to use, to solidify it, to finish it.
>
> For returning a previous value, explicitly use a get_set method, that itself
> explicitly calls get and then set, as then both get and set can be overridden.
>
> Any writer should return some (established or promised) final state in some way,
> either inside the underlying (log-)object, or directly. Avoid global variables.
>
>
> -- Ruud
>
>
> Random chaining example:
>
> my $sql= SQL -> from(t1 => "X") -> join(t2 => "Y", -on => {"t2.[% t1.name_lc
> %]_id" => "
t1.id"}) -> where(...) -> select(...) -> group_by(...) -> having(...)
> -> order_by(...);
>
> my $rv= $sql -> prepare(...);
>
> if ($rv->err) {
> ...;
> }
> else {
> $rv= $sql -> execute;
> }
>