|newbie question: type checking firstname.lastname@example.org (2001-11-08)|
|Re: newbie question: type checking email@example.com (Matthias Blume) (2001-11-08)|
|Re: newbie question: type checking firstname.lastname@example.org (Christian Bau) (2001-11-08)|
|From:||Christian Bau <email@example.com>|
|Date:||8 Nov 2001 23:20:38 -0500|
|Posted-Date:||08 Nov 2001 23:20:38 EST|
guilaume bour wrote:
> In the next example, we can't determine the type of the variable 'res'
> after the instruction 'if' (it can be a string as well as a float):
> if(div = 0)
> res := "division by 0"
> res := 4/div
> So I would like to know if there is a way to avoid(even suppress) such
If you download the Java language specification from
ftp://ftp.javasoft.com/docs/specs/langspec-1.0.pdf you will find that
a Java compiler has to keep track of which variables have defined
values and which ones have not; the algorithm for that is described in
this document (interesting to read if you are interested in
programming languages anyway).
If you take the same algorithm, but examine not only whether a
variable is defined or not, but also which type it has at which
point. Then if control flow merges from to branches, for example at
the point of the endif in your example, if a variable has different
types in both branches then it becomes undefined. So your example
would not be illegal, but after the endif res is actually undefined
and if the programmer tries to use it then your compiler should give
an error message. I would assume that you have to give an error
message anyway if an undefined variable is used, because in your
language an undefined variable wouldn't have any type.
Return to the
Search the comp.compilers archives again.