Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Bug Reports / Type declarations inside subroutines don't work

Author
Message
Rich Dersheimer
AGK Developer
14
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 3rd Sep 2010 21:22 Edited at: 3rd Sep 2010 21:29
I searched this board, but I didn't find this particular bug, maybe I missed it.

It seems a type can only be declared in a subroutine if it is declared global.

This will not compile...



and this will not compile...



but THIS will compile...


It compiles fine with array types, I imagine because arrays are all global. Apologies if this has already been covered.

EDIT: forgot to say, it's DBP version 75

Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 4th Sep 2010 00:21 Edited at: 4th Sep 2010 00:36
It makes no sense to put a type declaration in a subroutine, it's not something that needs to be executed at run-time.

As for what is causing the error, it's because the compiler doesn't know that richie should be the type specified (unless it is declared global, which means the variable with that name will always have that type, from the very start of the program). In the first two snippets at line 2 you're trying to print out the contents of what is actually an integer variable.

In short, you can't access a variable how you want and then later say "oh by the way, here's the variable type".
Rich Dersheimer
AGK Developer
14
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 4th Sep 2010 00:42
Quote: "It makes no sense to put a type declaration in a subroutine"


Oh. Where should I put them then? At the top of my code? Or maybe in an include file? I kind of like having them in a subroutine away from my main loop, but I guess I can give that up, if needed.

Doesn't the compiler make more than one pass? Seems like it could pick up the declaration then, but maybe only the "globals".

Thanks for the response

Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 4th Sep 2010 00:48 Edited at: 4th Sep 2010 00:48
As far as I'm aware it's treated sort of like a function - it always exists, in any scope. It doesn't need to be executed, so it should be possible to put the type declaration anywhere (even at the very end of the source file).

Quote: "Doesn't the compiler make more than one pass? Seems like it could pick up the declaration then, but maybe only the "globals"."


It does, but the problem with your code is that according to the rules you're actually accessing an integer variable. If you want it to be anything else, it must be declared as this above (in code) where you access it. It's different for a global since (like a function) it's a kind of constant-entity which exists all the time, from any scope (meaning the compiler can assume that it will always be that type).

Login to post a reply

Server time is: 2024-04-19 10:11:10
Your offset time is: 2024-04-19 10:11:10