In learning C#, I was (among other things) annoyed at having to write
Dog skip = new Dog();
while in C++,
Dog skip;
is sufficient. But before getting very far, I realized that in Perl we have to call new (or whatever) on everything, too. Ah, but the type is still named only once:
my $skip= Dog->new;
since the variable is not typed.

So, if Perl 6 is going to have much stronger compile-time awareness of types, perhaps we should have (to use the familiar Perl 5 syntax)

my Dog $skip; $skip->bark;
work properly.

How?

My first thought was to have a typed variable autovivify to a reference of the proper type. So, since bark() is called on undef, it creates one first. Assigning a value to $skip at any point before its used would suppress that behavior.

We already autovivify undef's based on how they are used: $x->[$y] will create an array ref, and we don't hear any cries from users about "well, maybe we wanted it to be undefined to indicate an error!" as in $x= find_my_array(); $x->[$y]; so extending this to members should not be any different conceptually. If you want to allow undef to mean an exception, check for it!

Another idea is to have a typed variable use a default initializer if one is not specified in the statement. So,

my Dog $skip;
would, as part of the "my" processing, call Dog->CREATE or somesuch. If you really wanted my Dog $skip= undef; you could say so.

—John


In reply to Fun with Typed Objects 1 by John M. Dlugosz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.