Forum Discussion
kbenson
15 years agoVisitor
Here's a few rules:
To clarify that last point, see the following
But come to think of it, a more general (but slower) global mechanism would be this:
That should allow global access from anywhere, even within a method (as long as those functions themselves are not turned into methods).
- Functions have separate scopes. - Every variable you use should be defined within the function, passed in as an argument, or accessed from m.
- Blocks do not have a scope of their own. - Variables defined within a block continue to exist outside that block. Normal for PHP users, different for many other languages. Personally, I find it causes more problems than it helps with me.
- Only count on globals if using procedural code - m is a handy built-in for accessing globals, but it's overloaded to be used for object attributes as well. Any object oriented code you write will have a hard time accessing those globals, unless you write some wrapper function(s) to access the globals for you.
To clarify that last point, see the following
function setFoo()
m.foo = "bar"
end function
function getFoo()
return m.foo
end function
object = {
foo: "not bar"
getObjectFoo: getFoo
}
' set global m.foo = bar
setFoo()
' print global m.foo
print getFoo() ' prints "bar"
' print object foo
print object.getObjectFoo() ' prints "not bar"
But come to think of it, a more general (but slower) global mechanism would be this:
function rdglobalGet(name as string) as dynamic
return m[name]
end if
function rdGlobalSet(name as string, value as dynamic) as dynamic
oldValue = m[name]
m[name] = value
return oldValue
end function
That should allow global access from anywhere, even within a method (as long as those functions themselves are not turned into methods).