Why does global initializers need to be constant?

BlitzMax Forums/BlitzMax Programming/Why does global initializers need to be constant?

Tibit(Posted 2005) [#1]
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?


Hotcakes(Posted 2005) [#2]
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?


rdodson41(Posted 2005) [#3]
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.


skidracer(Posted 2005) [#4]
Fixed in 1.12


LarsG(Posted 2005) [#5]
Skidracer: is 1.12 the next release, or is that the next one after that again?


Hotcakes(Posted 2005) [#6]
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.


skidracer(Posted 2005) [#7]
LarsG: 1.12 will be the one that hopefully gets released tomorrow.


Grey Alien(Posted 2005) [#8]
OMG!!!!


Leiden(Posted 2005) [#9]
What!!!?? You're not pulling fingers are you? Are you serious... Tomorrow what time?


Grey Alien(Posted 2005) [#10]
"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.


Leiden(Posted 2005) [#11]
*Hopeful*


rdodson41(Posted 2005) [#12]
why 1.12? shouldnt it be 1.11
sorta like the misterious newsletter 14 that never appeared


Leiden(Posted 2005) [#13]
Dunno, they also skipped a few versions when it was in Beta. All for the best :D


Perturbatio(Posted 2005) [#14]
I suspect the 1.11 was released as beta to the testers and after major updating, they felt it deserved a new version.


FlameDuck(Posted 2005) [#15]



Tibit(Posted 2005) [#16]
Nice!

And By "fixed" you mean that globals in types works the same as global globals, Right?


Hotcakes(Posted 2005) [#17]
I would assume they work the same as Function Globals... ie the same value for all instances of that object type.


Tibit(Posted 2005) [#18]
Which is the same as a "normal" global? or not?


Hotcakes(Posted 2005) [#19]
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.


Dreamora(Posted 2005) [#20]
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.


Hotcakes(Posted 2005) [#21]
I got confused when he mentioned global globals =]


Tibit(Posted 2005) [#22]
Then you haven't heard about local globals..


Hotcakes(Posted 2005) [#23]
<sniffs> You're scaring me.


Dreamora(Posted 2005) [#24]
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.