Replies: 5 comments 3 replies
-
That being said, var $x;
my $y = $x + 2; I think that should be fatal (or at least fatal in a scope where initialization is distinguished), but we're so casual about data, I doubt this will ever fly. |
Beta Was this translation helpful? Give feedback.
-
what kind of variable will |
Beta Was this translation helpful? Give feedback.
-
On 2023-06-02 11:27, Ovid wrote:
Currently, Perl has no distinction between undefined and uninitialized.
|[...] |
That causes a problem for runtime data checks ... because |my $var
:of(INT);| has an undefined value thus it blows up.
I see undef as a state, not as a value.
Both
$x = undef;
undef $x;
make $x to have the (value-less) undef-state.
The consequence of that is that I favor using default (obviously
defined) values for non-undef types.
The default default then represents EMPTY, being 0 for numbers, q{} for
strings, etc.
For objects, EMPTY can be a blessed reference to an undef scalar, or to
an empty hash or array, etc.
…-- Ruud
|
Beta Was this translation helpful? Give feedback.
-
On 2023-06-02 13:21, Branislav Zahradník wrote:
endless (in p5p) discussion - value exists but is not defined vs value
doesn't exist at all
Here it can be slightly mitigated by introduction of special handling
of |undef| - when you read variable, which has value |undef|, script
will die unless |undef| is valid value. (instead of currently optional
warning)
Yeah, so undef is not a value but a state. :)
There are many sides to this. For example from perlguts:
In perl 5.18 and earlier, AVs use
&PL_sv_undef as a marker for indicating that an array element has not
yet been initialized. Thus, "exists $av[0]" would be true for the above
Perl code, but false for the array generated by the XS code. In perl
5.20, storing &PL_sv_undef will create a read-only element, because the
scalar &PL_sv_undef itself is stored, not a copy.
…-- Ruud
|
Beta Was this translation helpful? Give feedback.
-
Closing this for now because it's a distraction. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently, Perl has no distinction between undefined and uninitialized.
That causes a problem for runtime data checks. Most languages allow you to declare a variable and later assign a value to it:
We can't easily do that because
my $var :of(INT);
has an undefined value thus it blows up.A
var
keyword would act likemy
, but allow us to know if the variable has ever been initialiazed. Once initialized in any way, it can never be uninitialiazed unless you sayundef $x
(not$x = undef
). Abuiltin::initialiazed
could help here.That would make it trivial to do this:
That would raise questions about how
var $x
becomes initialized.This is out of scope for the data checks MVP, but would it be worth considering later?
Beta Was this translation helpful? Give feedback.
All reactions