Re: can you check for assignment?
- Posted by "Boehme, Gabriel" <gboehme at POBOXB1.HQ.MSMAIL.MUSICLAND.COM> Aug 03, 1999
- 523 views
Terry wrote: >Is it possible to check if a *local* variable has been assigned a value >without crashing the program if false, as in the following example? No, it is not possible for *any* variable, regardless of scope. Not only that, but you're not using a local variable in your example. See section 2.4.2 ("Scope") in REFMAN.DOC for the proper definitions of global, local, and private. >procedure get_this(integer info) > > integer last_time > > if info = last_time then > do_this > else > do_that > end if > > last_time = info >end procedure > >This proc would crash on the *first* use. I tried > if integer(last_time) >which also crashes. >I don't really want to declare last_time as global because I have far too >many globals already (which I'm trying to tidy up). >Does declaring last_time invalidate my cause anyway, i.e. will it's value >be cleared everytime the proc is run? Yes. This code will crash *any* time you run it. You have defined "last_time" as a *private* variable, not a local one. In Euphoria, private variables do not exist until the routine is called. At the end of the routine, they are removed from memory. Local variables, on the other hand, exist outside of your Euphoria routines, and only within your source code member. These are only removed from memory when your program ends. [NOTE: You seem to be using "local" to mean "private", and "global" to mean "local". Again, see section 2.4.2 of REFMAN.DOC.] For what you want to do here, you will have to do two things: 1) Move your "last_time" variable definition to *before* the procedure declaration. This will make your variable *local*, rather than private. 2) Since there is no way to look at the value of any unassigned variable without crashing the program, you will have to come up with another way of accomplishing what you want here. One way is to immediately assign a "dummy" value to your variable after declaring it. This "dummy" value will indicate to your program that no legal value has been assigned to this variable yet. -- Example: assuming that "info" in get_this() will never be negative, -- we can start with a negative last_time value. integer last_time last_time = -1 procedure get_this(integer info) if info = last_time then do_this else do_that end if last_time = info end procedure Another way to do this is to create an additional flag variable to indicate whether or not your variable has been assigned to yet. [NOTE: for your given example, this is definitely the less efficient method, but in other situations it is the most sensible solution.] -- Example: using a boolean flag along with our variable constant TRUE = 1, FALSE = 0 type boolean(integer x) return x = TRUE or x = FALSE end type integer last_time boolean last_time_flag last_time_flag = FALSE procedure get_this(integer info) if last_time_flag = FALSE then do_this elsif info = last_time then do_this else do_that end if last_time = info last_time_flag = TRUE end procedure Hope this helps, Gabriel Boehme