You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Scalar:D type constraint doesn't work on a variable:
my $v = 3
my $s = $v.VAR
$s.WHAT.say
(Scalar)
$s.DEFINITE
True
my Scalar:D $x = $s
Type check failed in assignment to $x; expected Scalar:D but got Int (3)
in block <unit> at <unknown file> line 1
$s is a defined Scalar object, so should satisfy the Scalar:D type
constraint, but it is being rejected. The error message erroneously
refers to the value contained in the Scalar. As this suggests, if the
Scalar happens to contain a defined Scalar object (such as by "$v =
$s"), then the variable initialisation succeeds. The same goes for
assignments to the Scalar:D variable after successful creation: the
assignment is permitted iff the value being assigned is a defined Scalar
that contains a defined Scalar value (regardless of the value contained
in the inner Scalar).
A Scalar:D type constraint on a subroutine parameter works fine: it
accepts a value iff the value is a defined Scalar (regardless of the
value that the Scalar contains).
There is related misbehaviour that causes variables to accept values
that they should reject:
my $v = 5
5
my $s = $v.VAR
5
my Int:D $x = $s
5
$x.WHAT.say
(Scalar)
$v = Int
my Any:U $x = $s
$x.DEFINITE
True
The general pattern is that if a variable has a type constraint that
includes a definiteness qualifier, and an attempt is made to assign a
Scalar value to the variable, then the type constraint gets applied to the
value contained in the Scalar rather than to the Scalar itself. If the
type check against that value passes, then the Scalar value is assigned to
the variable, regardless of whether the Scalar itself satisfies the type
constraint. Type constraints that do not have definiteness qualifiers
work as expected, and so do type constraints on subroutine parameters.
Migrated from rt.perl.org#128407 (status was 'new')
Searchable as RT128407$
The text was updated successfully, but these errors were encountered: