oshun icon indicating copy to clipboard operation
oshun copied to clipboard

how to treat array access to not-permitted elements?

Open happy-barney opened this issue 1 year ago • 2 comments

Example:

my @list : (Array[ max_length (7), Int ];

if ($list[8]) { # should this fail?
}

happy-barney avatar Jun 02 '23 11:06 happy-barney

On 2023-06-02 13:27, Branislav Zahradník wrote:

Example:

|my @list : (Array[ max_length (7), Int ]; if ($list[8]) { # should this fail? } |

Would IMO be great if <$list[7]> then is a "compile-time" error.

So yes, IMO it should "throw an exception", if not at "compile-time" then at least at "run-time".

But then what about:

my $t :UInt8; $t |= 256;

-- Ruud

druud avatar Jun 02 '23 12:06 druud

In Perl, our data isn't just the "type" of the data (SV, IV, PV), but also the "shape" of the data (scalar, array, hash). I think any checks would have to consider both. Thus, we need array out-of-bounds errors.

Also, the MVP will not have compile-time errors because we're trying to constrain the scope as much as possible. I do see room for compile-time errors later.

Also, as for @druud's query regarding this:

my $t :UInt8;
$t |= 256;

It's a non-issue because the my $f :UInt8; would throw an exception because undef doesn't meet the constraint.

But if we have var:

var $t :of(UInt8);
$t |= 256;

The error would appear on the second line because you can coerce an undefined value, but not an uninitialized one. However, that would like leak out of the existing scope and cause grief elsewhere in unsuspecting code (which it should do, to be fair), but people would scream, so I'm pretty sure that var is not just out of scope for this MVP, but also impossible to get P5P to accept (what would happen with $array[$i] where $i is an uninitialized var? I should throw an exception).


As an aside, if we do attributes for the checks, we can't do :UInt8 because there are so many checks that we'd have massive pollution of the attribute "namespace" and would be much more like to cause conflicts with existing code.

Ovid avatar Jun 04 '23 06:06 Ovid