Why does global initializers need to be constant?
BlitzMax Forums/BlitzMax Programming/Why does global initializers need to be constant?
| ||
Type Test Global List = New TList 'Error Field List = New TList 'Works EndType Also can someone explain this: Type Test Field List = New TList Field Flag = Add( 30 ) Field TestField:Test = Create( 1 ) Function Create:Test( Flag ) NewTest:Test = New Test NewTest.Flag = Flag Return NewTest EndFunction EndType Print "START" T:Test = New Test Print T.Flag Print T.TestField.Flag Function Add( T# ) Return T#+10 EndFunction The function Add being called in the type as default value works with no problem but when it returns a type it exits with no error. Samething about globals is that they does not work with a create function. Why do globals need to be constant in type initziation? |
| ||
They don't. I have used Globals in conjunction with function calls that load an image and return a custom type (not TImage, but it may as well have been). The fact that you're experiencing problems is just weird. You using Strict? |
| ||
The globals have to be constant because they are kept as data in memory. Basically in the data section of the program, where all the global variables are kept there is a variable called, __bb_Test_List, and the compiler just makes references to Test.List go to that. So since it is hardcoded into the program, it cannot be changed. Check out the assembly code if you dont get what im saying. |
| ||
Fixed in 1.12 |
| ||
Skidracer: is 1.12 the next release, or is that the next one after that again? |
| ||
Bah look at me... I'm talking about Global in function calls and this topic is clearly about Global inside types... I should get more sleep. |
| ||
LarsG: 1.12 will be the one that hopefully gets released tomorrow. |
| ||
OMG!!!! |
| ||
What!!!?? You're not pulling fingers are you? Are you serious... Tomorrow what time? |
| ||
"what time" haha, have you been in software for long? I'm amazed a day let alone week can be given. Anyway it's cool news. |
| ||
*Hopeful* |
| ||
why 1.12? shouldnt it be 1.11 sorta like the misterious newsletter 14 that never appeared |
| ||
Dunno, they also skipped a few versions when it was in Beta. All for the best :D |
| ||
I suspect the 1.11 was released as beta to the testers and after major updating, they felt it deserved a new version. |
| ||
![]() |
| ||
Nice! And By "fixed" you mean that globals in types works the same as global globals, Right? |
| ||
I would assume they work the same as Function Globals... ie the same value for all instances of that object type. |
| ||
Which is the same as a "normal" global? or not? |
| ||
A Global from the main code means a variable that is automatically shared/accesible throughout any section of the code. A Global inside a Function means a variable that can only be accessed by that function, but the variable holds it's value between calls to that function. ie the value when the variable is initialised is 0 (unless otherwise stated), when the functin ends it's 5 - next time you call the function the variable holds the value 5 again. I'd assume a global inside a type would be a variable whose value holds the same for all instances of that object - say you have two references to that object - if you change the global variable in one reference, it automatically becomes that value in the other object as well. |
| ||
And by fixed he meant what mark mentioned months ago: initializing globals when declaring them in your types will be possible. So const initializer error won't show up anymore. |
| ||
I got confused when he mentioned global globals =] |
| ||
Then you haven't heard about local globals.. |
| ||
<sniffs> You're scaring me. |
| ||
local globals are function globals or type globals. They are only globals within their scope: Function global: This is something like a changable function constant. You can set this global and each time the function is called it will still have the same value as the last time this function was executed and the function set. This is a very good thing for counters and other things. type globals: this globals are unique to ALL objects you create of this type. the most common usage for example is the type list, that is used to handle all objects of this specific type. |