CO538 Anonymous Questions and Answers Keyword Index |
This page provides a keyword index to questions and answers. Clicking on a keyword will take you to a page containing all questions and answers for that keyword, grouped by year.
To submit a question, use the anonymous questions page. You may find the keyword index and/or top-level index useful for locating past questions and answers.
Keyword reference for global-variables
2003 |
I have:
VAL INT state IS 0:
I'm hoping this is like an external field in Java, but I can't work out how to alter the value of state. ``state := 1'' and many other ways I can't get to work. How can I alter the value, or is there another way or storing a value outside of a proc.
In occam, the only way you can make a variable global to a PROC is to have it in-scope. The `VAL' qualifier in occam effectively means constant, so your `state' isn't actually a variable -- it's a constant equal to zero. Removing the `VAL' won't work either, since occam does not permit variables to be declared at the outermost level.
You can do this sort of thing, but it probably won't work in the way you expect it to.. e.g.:
PROC thing (...) INT state: PROC fiddle.state (VAL INT new.state) state := new.state : ... body of `thing' :
In this code, the PROC `fiddle.state' has access to the `state' variable. However, the compiler's alias and parallel usage checking might prevent you from using `state' in the `body of `thing'' -- depending on usage of `fiddle.state'. In short, this is generally not a good way forward.
If you need a state variable for a PROC, just declare it inside the PROC and initialise suitably, e.g.:
PROC thing (...) INT state: SEQ state := 0 -- default state ... body of `thing' :
Only useful if the `body of `thing'' is long-lived, however.
Keywords: global-variables
I was wondering if it is possible to create a global boolean value that can be changed. I know that you can write:
VAL BOOL name IS TRUE:
and this can be used by any method, but you can't change this. So is there another version of writing this which will allow me to change it?
No; the compiler will not allow you to declare variables at the outermost level.
If you REALLY want to do this, you could re-structure your program so that everything is inside the main PROC; for example:
PROC main (CHAN OF BYTE in, out, error) BOOL b: -- local to main, but -- global to set & clear PROC clear () -- nested PROCs b := FALSE : PROC set () b := TRUE : SEQ -- body of main clear () set () :
But this sort of global variable is a very Bad Thing!
Any attempt to use such a variable in parallel (that is not CREW) will result in the usual compiler errors.
Keywords: global-variables
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. |