On Fri, 27 Feb 2015, Michael Bullman wrote:
I know you can force type declarations using '::'
so
my_int_var::Int64
my_float_var::Float64
now my_int_var requires an Int64 type, and my_float_var requires a Float64 and
both will float an error if you attempt to set them equal to
a different type. Although, in all honestly, I'm not sure if Float64 would cast
an Int to a float or not. But for user defined types this
works as expected.
Sorry, I problably did not explain my request quite well, I will
rephrase and illustrate with the example of Perl.
What I am looking for is a compiler option/pragma that will have the
compiler emit an error when a variable is used without having been
declared previously the way you talk about. A way to make sure all
variables are declared.
In Perl, you can do:
========0=========
$i=5;
print $i;
==================
But in order to prevent typos in variables, to prevent forgetting to
rename one occurence of a variable, and other mistakes that will generally
only blow up at runtime (the kind of bugs which can be really really
painful to find out), you can set the pragma "use strict;", that forces
you to declare the variable before (or while) you assign it:
========1=========
use strict;
$i=5;
print $i;
==================
=1= will fail for each undeclared variable, so twice here:
Global symbol "$i" requires explicit package name at test_strict_1 line 3.
Global symbol "$i" requires explicit package name at test_strict_1 line 4.
Execution of test_strict_1 aborted due to compilation errors.
You have to declare the variable $i before using or assigning it:
========2=========
use strict;
my $i;
$i=5;
print $i;
==================
========3=========
use strict;
my $i=5;
print $i;
==================
=2= and =3= are OK, and will emit an error if you try to "print $j;"
or even just "$j=6;" by mistake somewhere else because $j is not declared.
Goodbye,
Stéphane.