Global / Local variables

Previous topic - Next topic

Moru

When I declare a variable (by mistake) to have the same name both both local and global I get into problems with the compilers if the global is a type and the local is a bool. Example follows:

Code (glbasic) Select
// --------------------------------- //
// Project: Test14
// Start: Sunday, August 24, 2008
// IDE Version: 5.360


TYPE TEST
cd = 1
ENDTYPE

GLOBAL k AS TEST

WHILE TRUE
PRINT "Hello world", 50, 50
SHOWSCREEN
WEND

END


FUNCTION a:
LOCAL k

k = KEY(208)
ENDFUNCTION


Compiler output:
Quote
_______________________________________
*** Configuration: WIN32 ***
precompiling:
GPC - GLBasic Precompiler V.2008.210 - 3D, NET
Wordcount:6 commands

compiling:
C:\DOCUME~1\LOCALS~1\Temp\glbasic\gpc_temp0.cpp: In function `DGInt __GLBASIC__::a()':
C:\DOCUME~1\LOCALS~1\Temp\glbasic\gpc_temp0.cpp:26: error: no match for 'operator=' in 'k = __GLBASIC__::KEY(2.08e+2)'
C:\DOCUME~1\LOCALS~1\Temp\glbasic\gpc_temp_class.h:12: note: candidates are: __GLBASIC__::TEST& __GLBASIC__::TEST::operator=(const __GLBASIC__::TEST&)
*** FATAL ERROR - Please post this output in the forum
_______________________________________
*** Finished ***
Elapsed: 0.5 sec. Time: 07:05
Build: 0 succeeded.
*** 1 FAILED ***

Schranz0r

Thats no bug!

Global you can use everywhere!
Local you can use in a loop/For-Next ect...

So, if you us a Global k as function and set a local k, the compiler returned an FATAL ERROR ;)
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

Schranz0r

#2
GLOBAL k AS TEST // Set "k" to Global, so you can use this "k" in a function. Its a "typehandle"!

If you use "k" for example with : PRINT k,10,10

You get a error! ;)

"k" exists over the whole project as "typehandle"!
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

Kitty Hello

#3
that _is_ a bug. Because the local definition of "k" should have more priority. I'll try to investigate.


...ouch!
The compiler comes across a variable names "k" and finds, that k is of TYPE TEST, and makes the local variable of type TEST, too. Bad.

Schranz0r

->GLOBAL<- k AS TEST

@ Germot:

Das ist doch normal, das eine Globale über das ganze Projekt benutzt werden kann, also ist es kein Bug.
Weil wenn ich eine Global k = 10 mache , dann kann ich die in der Funktion auch benutzen..
Also ich denke das ist so schon OK!
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

Kitty Hello

Code (glbasic) Select

GLOBAL g=10

foo()
DEBUG g

FUNCTION foo:
LOCAL g
  g = 15
ENDFUNCTION

perfect code. output=10.

Moru

I'm perfectly aware that it works, Gernot. That is how I want it to work. The point is if you have a type with the same name as a bool it doesn't work... The program doesn't even run, the compiler stops you.

If this IS how you want it to work, I would expect a bit more helpfull error message, not just "post on the boards and get publicly humiliated in german"

Hemlos

#7
explicitly specify works? ooops no it doesnt....the problem is in k...this fails too...
Code (glbasic) Select

TYPE TEST
c = TRUE
ENDTYPE

GLOBAL k AS TEST

PRINT ""+k,10,10

SHOWSCREEN
Bing ChatGpt is pretty smart :O

Hatonastick

k or single letter variable names?

I only ask because I had some weirdness pop up while mucking about with types while trying something else out, and the variable names I was using were a, b, c etc. (Hmm I think I used k too :)).  Might have just been my bad code though.  Anyway I remember I got an error similar to the one Moru has quoted.

Kitty Hello

the problem is, that a global with a different TYPE already exists.

Hatonastick

#10
[Edit]: Um never mind. :)