I'm asking people who are familiar with the ideas in extant Perl, how it works, what you can do with it, what its limitations are; in addition to speculating about how it should be in Perl 6. The synopses assumes "same as now" except where it gives hints and contradictions.

Consider:

my Int $x = 17; my Int $y is readonly := $x;
This indicates that readonly is an attribute of the symbol, not the container. The two symbols, $x and $y, have different attributes, rw and readonly respectively.

This situation must be allowed, because it is exactly the same as

sub foo (Int $y) { ... } my Int $x = 17; foo($x);
which is also the same as
my Int $x = 17; $x -> $y { ... }
and likewise when $x is really one object in a multi-item container such as a list, when doing a for loop.

So, what happens in the reverse situation?
my Int $x is readonly = 17; my Int $y := $x;
I think this should be a compile-time error. You can't relax the constraint when creating the alias.

In this case, it is behaving like a type but not like a class, exactly like subset (and where clauses) do. Regardless of whether constraints are part of the symbol, container, or value type, they are checked when that symbol is assigned to, and likewise checked when the symbol is bound. You have the same issue with subsets, to ensure that all aliases agree with the existing constraints. Or rather, you can add to them but not take away.

Meanwhile, some people want to write functions that might take a writable lvalue, or might not. As I see it, this is an error:

sub foo (Int $y is rw) { ... } foo(17); my Int $x is readonly = 17; foo($x);
Is the first call passing a true constant, as opposed to a handle without write access permission? That is, is there a deeper meaning of constant available as well that lets you do constant folding, shared thread data, caching, etc.?

Anyway, if the calls fail at compile time, it is because the readonly is being treated as part of the type, just like subsets as I mentioned earlier. So if someone really wanted to have optional write-back ability, he could write:

sub foo (Int|constInt $y) { ... }
where you had some way of defining the named type constInt (not a class, but type-like in the same manner as subset). More sanely perhaps you could use that in a multi to distinguish a readonly and rw form of the function.

Can you declare constInt in Perl 6 as it is defined thus far, using where or other language features?

I'll stop now and listen to opposing views and inspiration.

—John


In reply to Just what is 'readonly' in Perl and Perl 6? 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.