develooper Front page | perl.perl6.users | Postings from July 2021

Re: Why does .new initialize?

Thread Previous
Matthew Stuckwisch
July 19, 2021 17:40
Re: Why does .new initialize?
Message ID:
In general, the idea of initialized doesn't mean a lot in Raku, at least
not at the language level.

At any given time, any variable has a value. By default, if you've typed a
variable, it's initially set to the type itself (Any is the default type,
so the default default value). The only exceptions are native types which
are either 0 or the empty string, depending, or if you've added a trait to
override the default value. Note the following, which may be a bit
surprising to you:

    my Int $a;
    my $b = Int;
    my $c;
    say $a === $b === Int; # True
    say $c === $a; # False

$c's value here is (Any), but both $a and $b are (Int).

.new calls self.bless, which handles all of the allocation stuff behind the
scenes, but —importantly— each attribute, like any variable, will always
have a value (it just might not be defined), such that

    class Foo {
        has Rat $.bar;
    say === Rat; # True
    say; # False

In some classes, undefined values for those attributes may not make sense,
and each can make its own decisions how to handle when lacking defined

Hope that makes sense,


On Mon, Jul 19, 2021, 19:10 Peter Scott <> 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 <> 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;      # 0
>     dd;      # 0e0
>     dd;  # <0+0i>
>     dd;      # ""
> 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

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About