TYPE and CONST within SUB/FUNCTION - Printable Version +- QB64 Phoenix Edition (https://staging.qb64phoenix.com) +-- Forum: Chatting and Socializing (https://staging.qb64phoenix.com/forumdisplay.php?fid=11) +--- Forum: General Discussion (https://staging.qb64phoenix.com/forumdisplay.php?fid=2) +--- Thread: TYPE and CONST within SUB/FUNCTION (/showthread.php?tid=1835) |
TYPE and CONST within SUB/FUNCTION - TerryRitchie - 07-09-2023 A while back I accidentally discovered that CONST can be used in SUBs and FUNCTIONs to create local constants. You can even use the same CONST variable name from the main code to create another unique local constant. I was wondering if this worked with TYPEs. It appears it does not. While you can create a user defined TYPE within a SUB or FUNCTION it will always be seen globally. Is this correct behavior or should the user defined TYPE be local to the SUB and/or FUNCTION? If the user defined TYPE is always to be seen globally then is it an oversight in the IDE to allow TYPEs to be created within a SUB or FUNCTION? Code: (Select All)
RE: TYPE and CONST within SUB/FUNCTION - SMcNeill - 07-09-2023 TYPEs are always global, as per the odd behavior which QB45 had with them back in the day. I think the reason was that some of the programmers back in those days used to write TYPE libraries and then just include them willy-nilly into the code whenever they needed them. (Think of a TYPE point: X AS INTEGER: Y AS INTEGER: END TYPE) No matter where you put those type statements, they tend to produce global variable types for use. And, honestly speaking, I'm not certain if you'd want it any other way. Can you imagine the confusion of writing a library with a POINT type, and making it 2D, while someone else creates a library with a POINT type and makes it 3D? If that's not a recipe for confusion and utter programming frustration, I don't know what would be! RE: TYPE and CONST within SUB/FUNCTION - Kernelpanic - 07-09-2023 Quote:A while back I accidentally discovered that CONST can be used in SUBs and FUNCTIONs to create local constants. You can even use the same CONST variable name from the main code to create another unique local constant.If a constant is declared within a procedure or function, it has the status "local" and is only known there. Otherwise a constant is declared globally; in the main program. Type . . . End Type definitions can not be declared in functions or procedures; the best place for them is at the beginning of a program - right after the constant declaration. Actually clear, because what's the point of a type declaration in a procedure or function? However, type definitions can be used as argument types of procedures and functions. From MS-DOS QBasic - The Technical Reference; The Wait Group 1991/92 RE: TYPE and CONST within SUB/FUNCTION - bplus - 07-10-2023 I am thinking, being able to Define a Type in a Sub or Function could be handy for Library Code might even save you from having to create a .BI file. Run an InitLibr sub to start Type(s) maybe? RE: TYPE and CONST within SUB/FUNCTION - TerryRitchie - 07-10-2023 (07-09-2023, 11:05 PM)Kernelpanic Wrote: Type . . . End Type definitions can not be declared in functions or procedures; the best place for them is at the beginning of a program - right after the constant declaration.So perhaps the IDE accepting the creation of user defined TYPEs in SUBs and FUNCTONs is not desirable then. Should the IDE throw an error if TYPE ... END TYPE is used within a SUB or FUNCTION? I don't believe I've ever seen QBASIC/QuickBASIC 4.x code that used CONST or TYPE in a SUB or FUNCTION. KernelPanic's quote from the Qbasic tech reference seems to confirm at least that TYPE ... END TYPE was not allowed. The weird behavior of CONST within in a SUB/FUNCTION got me thinking about all of this. UPDATE: Ok, I fired up QuickBasic 4.5 and verified that you can indeed create TYPEs within SUBs and FUNCTIONs within the IDE and the IDE will even appear to use them as being global. However, when you try to compile it fails. TYPE ... END TYPE must be in the main program module. RE: TYPE and CONST within SUB/FUNCTION - bplus - 07-10-2023 Good new code will be compatible with old but be more flexible for new stuff. RE: TYPE and CONST within SUB/FUNCTION - Kernelpanic - 07-10-2023 Example of connecting two string constants in the main program and a function (procedure works the same way). Code: (Select All) 'Beispiel fuer Nutzung einer Konstanten in einer Funktion(Prozedur) - 10. Juli 2023 RE: TYPE and CONST within SUB/FUNCTION - SpriggsySpriggs - 07-10-2023 I would love to have local TYPEs rather than all of them be global. RE: TYPE and CONST within SUB/FUNCTION - TerryRitchie - 07-11-2023 (07-10-2023, 06:53 PM)SpriggsySpriggs Wrote: I would love to have local TYPEs rather than all of them be global. I was hoping for the same thing when I went down this rabbit hole. RE: TYPE and CONST within SUB/FUNCTION - bplus - 07-11-2023 Playing around with Types defined in Subs and Terry's test code, I switched to testing Type values and Shared with Main: Code: (Select All)
Works as one might expect. |