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.

AppGameKit Classic Chat / Compiler error

Author
Message
george++
AGK Tool Maker
17
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 25th Jan 2017 18:43 Edited at: 25th Jan 2017 18:44
Please compile the following piece of code

You'll get an error
Ortu
DBPro Master
16
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 25th Jan 2017 19:32 Edited at: 25th Jan 2017 19:42
Yes, because you are effectively declaring:

1 as integer

In the type declaration for field3 which is also declared as a constant.

I would expect this behavior.

Remember, constants are not static variables, they do not have scope, they are literally replaced during compile. This means you cannot reuse a constant token as a variable name anywhere else.
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
george++
AGK Tool Maker
17
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 25th Jan 2017 20:57
Quote: "I would expect this behavior."

No I wouldn't. The field names of the types should be different only between the same type.
What if I have the following?


In the case above, the compiler does not give any error
Ortu
DBPro Master
16
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 25th Jan 2017 21:01 Edited at: 25th Jan 2017 22:07
Quote: "
Remember, constants are not static variables, they do not have scope, they are literally replaced during compile. This means you cannot reuse a constant token as a variable name anywhere else."


Read this part again.

Your second snippet works because it had no constant.

A constant is like a precompiler macro. All instances of the token get replaced with value as if it were literally typed in the source. Constants have no concept of scope and are replaced before any declaration or scoping of variable names are processed.

This:

#constant field3 1

Makes this:

field3 as integer

Into this:

1 as integer

Before the compiler even begins looking at variable names
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 25th Jan 2017 21:49
as workaround , just a underscore
#constant FIELD_3 1

type aType

field1 as integer
field2 as integer
field3 as integer

endtype
AGK (Steam) V2017.01.09 : Windows 10 Pro 64 Bit : AMD (16.12.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
george++
AGK Tool Maker
17
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 25th Jan 2017 22:48 Edited at: 25th Jan 2017 22:50
Thank you Markus for the tip.
@Ortu: I understand what you mean. I believe that the AppGameKit should be more clever and should distinguish the declarations of constants from the definitions of types even when they have the same names
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 26th Jan 2017 08:12
Quote: "I believe that the AppGameKit should be more clever and should distinguish the declarations of constants from the definitions of types even when they have the same names"


That would defeat the purpose of constants (for many languages, not just AGK). The important point about Ortu's advice is that constants in AppGameKit are a compiler directive, not executable entities. The constants are replaced with literals, which make the run-time execution faster.
For example it is valid to write the following, which would not work if you change the rules around constants:


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 26th Jan 2017 09:07 Edited at: 26th Jan 2017 09:16
me know constants only as values that can't be change (static), not as replacing source code.
#alias is a better name for BatVink example.
AGK (Steam) V2017.01.09 : Windows 10 Pro 64 Bit : AMD (16.12.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
george++
AGK Tool Maker
17
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 26th Jan 2017 14:32 Edited at: 26th Jan 2017 14:43
In C++ the #constant directive is equivalent to the #define which I think reflects better the BatVink example

Quote: "constants are replaced with literals, which make the run-time execution faster."

This is not true. The use of the #constant directive, at least in the Mouse example, helps to make the code more readable.
As far as I know, the final step is the linker, which will load onto the stack the same number of parameters in both cases (with or without the use of the #constant directive)
Ortu
DBPro Master
16
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 26th Jan 2017 18:14 Edited at: 26th Jan 2017 18:16
The effect and benefit of a constant depends on how it is used:

In Bat's sample of assigning a function (or other executable source), the benefit is readability and accessibility (you only need to change the constant definition to change what it does throughout the source vs having to change every occurrence if written literally throughout), it won't affect executable speed or memory footprint.

A flat value constant can provide readability, accessibility, performance speed, and reduced memory vs storing the value to a global variable that has to be held and repeatedly accessed.
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 26th Jan 2017 18:26
Just to show how you could almost write your own language. The revealing line is:

if mouseHidden = 1 then ShowMyMouse else HideMyMouse


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Ortu
DBPro Master
16
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 26th Jan 2017 18:56 Edited at: 26th Jan 2017 18:57
A way I commonly use them is short hand in array iteration:

http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 27th Jan 2017 10:42
@BatVink: That snippit looks familiar. lol
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 27th Jan 2017 12:32
It was in my temp project from the other thread, so I tweaked it
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 27th Jan 2017 12:52 Edited at: 27th Jan 2017 12:52
Demonstrating code reuse, needs to be a badge for that. lol
May i suggest this image:
Stab in the Dark software
Valued Member
21
Years of Service
User Offline
Joined: 12th Dec 2002
Playing: Badges, I don't need no stinkin badges
Posted: 27th Jan 2017 14:05
Badges?????????
The coffee is lovely dark and deep,and I have code to write before I sleep.
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 27th Jan 2017 14:33

Login to post a reply

Server time is: 2024-09-29 23:33:47
Your offset time is: 2024-09-29 23:33:47