Functions in Types?

Previous topic - Next topic

MrTAToad

Now, onto different variables in types :

Code (glbasic) Select
TYPE Ta
a

FUNCTION first%:
a=10.0
ENDFUNCTION

FUNCTION test%:a%
LOCAL b%

b%=a%
ENDFUNCTION
ENDTYPE


With this you get "error : redefinition as different type : a is of type DGInt".  Now there is a real "a" and an integer "a%".  The problem is that the scope rules are a bit different than usual.

It seems that when b%=a% is performed, the precompiler is checked the a against the a in the TYPE, which it should only be doing if SELF was used.

Schranz0r

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

you forgot the "self.". It's required!

MrTAToad

#63
Thats the problem - in this case, self shouldn't be used as I'm not using a in the TYPE...  I want to use the a% variable which would be a passed value.

a%
and self.a should be treated differently (especially as they are different types).

Kitty Hello

Code (glbasic) Select


TYPE Ta
   a // a# is a member of Ta

   // first% is a member function of Ta!
   FUNCTION first%:
      a=10.0 // a is not declared. A GLOBAL will be created
      self.a = 10.0 // a# member var is used now
   ENDFUNCTION
   
   FUNCTION test%:a%
   LOCAL b%

      b%=a% // assign LOCAL a%(Param) to LOCAL b%
   ENDFUNCTION
ENDTYPE

MrTAToad

#65
Yes, actually I did forget self :)

However, I do think global variables shouldn't interfere with passed variables - its a problem I've met occasionally with V7 :)

Might we worth adding a few descriptive error messages - for example : error: no `DGNat __GLBASIC__::Tb::moo()' member function declared in class `__GLBASIC__::Tb' really means that you've tried to define a TYPE twice...

MrTAToad

Indeed - it looks like passed variables are treated as global, hence the message.  Unfortunately that can be a bit of a problem at times.

Kitty Hello

It's not complaining about the passed variable. It's complaining about the variable it had to globally create above. Turn the explicit option on to see where is starts to trouble.

MrTAToad

One other thing - with the new type system, the compiler complains if you use INLINE...

Kitty Hello

Always or just sometimes? Please post an example then. It works for me here.

MrTAToad

Its generally all the time :

Code (glbasic) Select
INLINE
extern "C" char * tmpnam ( char * str );
ENDINLINE

TYPE Ta
FUNCTION moo%:
INLINE
int x,y;

tmpnam(NULL);
ENDINLINE
ENDTYPE


This generates :

C:\Users\Nicholas\AppData\Local\Temp\glbasic\gpc_tempg.cpp:19: error: `tmpnam' was not declared in this scope
C:\Users\Nicholas\AppData\Local\Temp\glbasic\gpc_tempg.cpp: At global scope:
C:\Users\Nicholas\AppData\Local\Temp\glbasic\gpc_tempg.cpp:26: error: expected `}' at end of input

With just :

Code (glbasic) Select
TYPE Ta
FUNCTION moo%:
INLINE
int x,y;
ENDINLINE
ENDFUNCTION
ENDTYPE


get the usual "error: `__l_dbg_cont' was not declared in this scope" error

Schranz0r

PLS use INLINE in a separate GBAS,
also the Type must be in there!
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

MrTAToad

I'm assuming that you would put it the source section and not the main file.

And the type is there  :S

Schranz0r

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

MrTAToad

No doubt the later versions of GPC.EXE fix the problem :)