Consider:
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.my Int $x = 17; my Int $y is readonly := $x;
This situation must be allowed, because it is exactly the same as
which is also the same assub foo (Int $y) { ... } my Int $x = 17; foo($x);
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 = 17; $x -> $y { ... }
I think this should be a compile-time error. You can't relax the constraint when creating the alias.my Int $x is readonly = 17; my Int $y := $x;
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:
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.?sub foo (Int $y is rw) { ... } foo(17); my Int $x is readonly = 17; foo($x);
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:
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?sub foo (Int|constInt $y) { ... }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |