Undefined values: Difference between revisions
Content added Content deleted
(Add Factor) |
(Add comment for Rust) |
||
Line 135: | Line 135: | ||
int main() |
int main() |
||
{ |
{ |
||
int junk, *junkp; |
|||
/* Print an unitialized variable! */ |
|||
printf("junk: %d\n", junk); |
|||
/* Follow a pointer to unitialized memory! */ |
|||
junkp = malloc(sizeof *junkp); |
|||
if (junkp) |
|||
printf("*junkp: %d\n", *junkp); |
|||
return 0; |
|||
}</lang> |
}</lang> |
||
Line 252: | Line 252: | ||
There is no undefined value in Déjà Vu. Instead, trying to access an undefined variable raises an exception. |
There is no undefined value in Déjà Vu. Instead, trying to access an undefined variable raises an exception. |
||
<lang dejavu>try: |
<lang dejavu>try: |
||
bogus |
|||
catch name-error: |
catch name-error: |
||
!print "There is *no* :bogus in the current context" |
|||
return |
|||
!print "You won't see this."</lang> |
!print "You won't see this."</lang> |
||
If you need to declare a local variable, but don't have a value for it yet, there is the standard function <code>undef</code>, which raises an exception when called but is an actual value that can be passed around and assigned to names. |
If you need to declare a local variable, but don't have a value for it yet, there is the standard function <code>undef</code>, which raises an exception when called but is an actual value that can be passed around and assigned to names. |
||
Line 1,236: | Line 1,236: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
Raku has "interesting" values of undef, but unlike Perl |
Raku has "interesting" values of undef, but unlike Perl 5, doesn't actually have a value named <tt>undef</tt>. Instead, several very different meanings of undefinedness are distinguished. First, <tt>Nil</tt> represents the absence of a value. The absence of a value cannot be stored. Instead, an attempt to assign <tt>Nil</tt> to a storage location causes that location to revert to its uninitialized state, however that is defined. |
||
<lang perl6>my $x; $x = 42; $x = Nil; say $x.WHAT; # prints Any()</lang> |
<lang perl6>my $x; $x = 42; $x = Nil; say $x.WHAT; # prints Any()</lang> |
||
Line 1,351: | Line 1,351: | ||
Done |
Done |
||
</pre> |
</pre> |
||
=={{header|Rust}}== |
|||
All variables in Rust must have defined type and value. |
|||
For the representation of a value which might not be present, the standard library offers wrapper type <code>Option</code>. |
|||
<code>Option</code> has two variants: <code>Some</code> (holding a value) and <code>None</code> (indicating the absence of any value): |
|||
<code>Option</code> is a regular <code>enum</code> type and there is nothing special about it, except for its broad usage. |
|||
Rust supports raw pointers. |
|||
Pointers are used for interoperability with other languages and for low-level unsafe operations. |
|||
Safe Rust code can't use pointers as effective use of a pointer (dereferencing it) requires <code>unsafe</code> block. |
|||
A pointer may have a ''null'' value: |
|||
<lang Rust>use std::ptr; |
|||
let p: *const i32 = ptr::null(); |
|||
assert!(p.is_null());</lang> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |