Matthew has provided some concrete examples of default initializations. I'd 
like to scratch the surface of more general problem: encapsulation. In many 
cases only a class knows it's real internal structure and can use this 
information to protect the data from misuse by 3rd party code which may not 
have the sufficient level of knowledge. If we deprive a class the right of 
self-initializing according what it knows will be a correct state then what 
remains of the encapsulation principle?

Let me make a guess. Perhaps the meaning of "new() should only allocate memory 
and never put anything in there" statement is a bit different. Perhaps the 
initial discussion was about the method new() in particular. It probably stems 
from the early days of Perl OO when more or less typical code was something like

sub new {
    my $self = bless {}, shift;
    $self->{foo} = 13;
    $self->{bar} = 42;
    return $self;
}

Then moving the initialization lines for attributes foo and bar out of new() 
makes full sense. One of the approaches taken before the Moose epoch was:

sub new {
    my $self = bless {}, shift;
    $self->init(@_);
    return $self;
}

This was solving some pretty much common errors in class design and allowed the 
child classes to implement their own init() methods without actually worrying 
about object's origins.

Getting back to Raku, the principle is carefully followed because it's new() 
doesn't do any actual object initialization. Instead it re-delegates it down to 
submethods BUILDALL, BUILD, TWEAK. Apparently, it is possible to literally 
follow the principle and split pure new and initialization code. But how much 
happy would you be writing something like Class.new.INITIALIZE(:foo(13), 
:bar(42)) every time? And how much sense would it make?

Best regards,
Vadim Belman

> On Jul 19, 2021, at 1:09 PM, Peter Scott <pe...@psdt.com> wrote:
> 
> Yes.  I'm agnostic on this point, but there was a time when some prominent 
> Perl contributors were dogmatic about it and I didn't know how widespread it 
> was.
> 
> Peter
> 
> On 7/19/2021 10:06 AM, Vadim Belman wrote:
>> 
>> Let me guess. The school prohibits object self-initialization? It has to be 
>> done by external code?
>> 
>> Best regards,
>> Vadim Belman
>> 
>>> On Jul 19, 2021, at 1:00 PM, Peter Scott <pe...@psdt.com 
>>> <mailto:pe...@psdt.com>> wrote:
>>> 
>>> On 7/19/2021 1:24 AM, Elizabeth Mattijsen wrote:
>>>> If .new wouldn't initialize a type to its basic instantiation, what would 
>>>> be the point of .new then?
>>>> 
>>>> FWIW, the same goes for:
>>>> 
>>>>     dd Int.new;      # 0
>>>>     dd Num.new;      # 0e0
>>>>     dd Complex.new;  # <0+0i>
>>>>     dd Str.new;      # ""
>>>> 
>>>> If you want to leave it undefined, don't call .new on it?
>>>> 
>>>> *confused*
>>> 
>>> Only that there's a vocal school of thought in O-O that says new() should 
>>> only allocate memory and never put anything in there.  Now I know that Raku 
>>> doesn't subscribe to that I have no problem.
>>> 
>>> Cheers,
>>> Peter
>> 
> 

Reply via email to