May 02, 2024, 09:00:37 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Scope resolution

Started by Ionic Wind Support Team, December 10, 2005, 08:37:32 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

Ionic Wind Support Team

Been working on scope resolution this evening.  When a class method has the same name as an imported function or external subroutine you have to tell the compiler which function to use when your within a class method.

So keeping with C like syntax I've used the double colon.  Illustrated in this code snippet, the code for GetClientRect from the window class.


DECLARE IMPORT,GetClientRect(hwnd as int,lpRect as RECT),INT;
window::GetClientRect(),RECT
{
RECT rc;
rc.left = 0;rc.top = 0;rc.right = 0;rc.bottom = 0;
if(m_hWnd)
::GetClientRect(m_hWnd,rc);
return rc;
}


In the code above we have a name conflict.  GetClientRect exists as both an imported DLL function and the class method.  Using a '::' in front of a function name tells the compiler you want global scope, and not local.

Without it you would get a 'wrong number of parameters' error since the class method takes precedence over the global imported funciton.

Just noting it here for future reference.

Ionic Wind Support Team

Parker

Just wondering, is there going to be a way to pass RECT structures to a function by doing something like this:
SomeFunction(RECT{0, 0, 500, 500});

Ionic Wind Support Team

Probably not right away.  Requires a lot of memory management for the temporary struct.

However once class constructors can accept parameters you could have a rect class

SomeFunction(CRect(0,0,100,100))

Which is the same thing.

Paul.
Ionic Wind Support Team

Parker

Cool, glad to know it'll work that way, so for now just a request in the future for the ability to pass structures like that. Thanks.

GWS

Will there be 'block' scoping as in C++ .. I've been reading againÂÃ,  ::) and it's a new one on me.

Also, I triedÂÃ,  ÂÃ, int x = 5; and it doesn't compile.ÂÃ,  Will this form of define and initialise be possible?

Graham :)
Tomorrow may be too late ..

Ionic Wind Support Team

Graham,
No to the block resolution.  Yes to the initialization. ;)

I've always considered block scoping more of a C++ trick than a feature.  In a subroutine the variable will always be located on the stack regardless of wether or not the compiler lets you access it.  It is also very confusing to new programmers, not to mention prone to causing bugs.

For example.


sub MySub()
{
int z,x;
    for(x = 0;x < 7;x++)
    {
       int z; // only accessable during the for loop
       z = x+100;
    }
    writlen(str$(z)); // uses the 'z' at the scope of the subroutine
}



Ionic Wind Support Team

Zen

This is good news. I like the idea of being able to define and initialize variables. Ive had many occasions when i have had about 50 lines of code in IBasic. 50 to define and 50 to initialize. I know you can do both on one line but i think im one of those code tidy freaks. lol

Lewis

ThadMiller

Quote from: Ionic Wizard on December 22, 2005, 06:55:47 AM
I've always considered block scoping more of a C++ trick than a feature.

But .Net also does block scoping, so it MUST be the right thing to do!  ::)

(sorry, couldn't resist  ;) )

Parker

I've seen it in freebasic:
scope
dim x as integer = 1 ' only available in the scope block
print x
end scope
print x ' will give an error since x doesn't exist


I wasn't aware that it existed in C++, although I've never tried to use it, it's probably just a better idea to use standard variables since that would get confusing though, I agree with Paul. Although having it as a feature never hurts if Paul wants to change his mind, or do something like scope{ ... }  ;)

srod

Sorry for drifting off topic a little and forgive the dumb question!

Paul, in the import statement below
DECLARE IMPORT,GetClientRect(hwnd as int,lpRect as RECT),INT;

How is it that we are not required to name the underlying dll file? Am I right in assuming that Aurora automatically links to the common system dll's?







Ionic Wind Support Team

It works just like it did in Pro.  There are import libraries in your 'libs' directory and a few of them are automatically linked against.  Such as kernel32.lib, user32.lib and gdi32.lib.

If you were to use a third party DLL you would need to create the import library with the tools menu and then include it with your project using a #use statement.

#use "somedll.lib"

Ionic Wind Support Team

srod

Thanks for the reply.  To be honest, I never really got to grips with IB pro, mainly because I was already very comfortable with the basic language I was already using and saw no real need to switch when both l anguages offered more or less the same functionality.

Aurora is a different kettle of fish, however, in that I think this is looking to be the perfect way into OO, the perfect complement to my existing development language.

I will purchase a licence when I get back to my broadband connection, after xmas.