Parameterising an application
Robert Cailliau
robert.cailliau at free.fr
Sat Jul 2 05:16:48 EDT 2005
Hello,
When building an app containing several stacks, one may want to use
some constants that are to be used by more than one of the stacks.
E.g. I may want to use a set of colours or some dimension such as a
fontsize that should be available everywhere.
Yet I do not want to declare the values more than once.
It seems I can do this in one of the following ways:
(1) declare and initialise a global variable, then declare and use it
in other stacks:
Mainstack:
global gRed
on Initialise
put "255,0,0" into gRed
end Initialise
Other stack:
global gRed
...
set the backgroundcolor of field "X" to gRed
...
(2) create and set a property of the mainstack and use it in other stacks.
Mainstack:
on Initialise
set the pRed of this stack to "255,0,0"
end Initialise
Other stack:
...
set the backgroundcolor of field "X" to the pRed of stack "Main"
...
(3) declare a constant and duplicate that line of code in all stacks.
Mainstack:
constant cRed = "255,0,0"
Other stack:
constant cRed = "255,0,0"
...
set the backgroundcolor of field "X" to cRed
...
Methods (1) and (2) have the disadvantage that I need to separate the
declaration from the value assignment and/or need to write code in an
initialisation routine. Method (3) is in some sense worse because
when I change my mind as to the value, I have to change it in all
stacks that use it.
It seems I cannot do it by some statement like:
global constant cRed = "255,0,0"
However, this restriction does not apply to handlers: If I write a
handler in the main stack's script and then call it from a substack,
I do not have to make any declaration in the substack. Similarly, it
is sufficient to write
start using stack <stackname>
to get access to all its handlers.
Maybe (suggestion) there should be a "global constant" statement,
such that any global constants are application-wide and this would
also apply to global constants declared in stacks used as libraries.
Since I designed and wrote my own languages and compilers (back in
the 70's!) and spent a lot of time thinking about modular
programming, I know that I'm asking for something that can only be
implemented efficiently in a fully compiled language and not in an
interpreted one (C uses the horrible "header files" for this
purpose).
But actually I do not care how global constants are implemented: it
will in fact have to be implemented in the same way as a global
variable with implicit initialisation, thereby excluding some
optimisations.
E.g. when I write
constant cMargin = 35
...
put 2*cMargin into lExtraSpace
...
Revolution can actually compile this into
put 70 into lExtraSpace
and this optimisation would no longer be possible with values of
global constants.
However, that is irrelevant: it is also impossible with any of the
other methods I gave.
What is much more important is that a global constant statement would
allow me to collect all the application-wide values in one place,
making the code cleaner and more robust.
There is one way in which I can use constant declarations right now:
I could write a handler that looks at all the scripts of all the
objects in my application and puts the block of constant declarations
at the start of the scripts (or some place marked in some
convention). This is rather more ugly though.
Or can I have this facility already but I missed some part of the
documentation?
Or has anyone a simpler and equally effective solution in terms of
existing syntax?
Robert.
More information about the use-livecode
mailing list