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 / When is an integer not an integer

Author
Message
PopsHugo
9
Years of Service
User Offline
Joined: 28th Jan 2015
Location: South Africa
Posted: 13th Feb 2015 12:03
I have a problem. The code is below.

Why is the compiler not accepting the integer icx in lines 17 onward?

Pops Hugo

tables.agc

16 icx as integer = 200
17 global backgroundicon as integer = icx
18 global clickanywhereicon as integer = icx : icx = icx + 1
19 global colorcrazeehicon as integer = icx : icx = icx + 1

Error messages:
Compilation failed.
tables:agc:17 error: Variable default value must be an integer literal or constant
tables:agc:18 error: Variable default value must be an integer literal or constant
tables:agc:19 error: Variable default value must be an integer literal or constant

Old grey plodder living in Cape Town
Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 13th Feb 2015 13:14
Someone will probably come along and correct this, as I'm not completely sure - but I think that you have to treat a Global as a changing constant - so you can't assign it with another variable, you'd have to assign an actual numeric value instead when declaring the global.

Like...

global backgroundicon as integer
backgroundicon = icx

I don't think AppGameKit is designed to use a variable in global deceleration, just an actual value or nothing at all. As I said though, not 100% sure, it's just something I do regardless.

I am the one who knocks...
unlikely
12
Years of Service
User Offline
Joined: 5th Mar 2012
Location: Ohio, USA
Posted: 13th Feb 2015 16:29 Edited at: 13th Feb 2015 16:32
I don't think it has to do with global scope. I believe the compiler doesn't want you to assign a typed variable with a non-literal at declaration. You can, however, do this with a dynamically typed variable...

e.g.
F as integer = 5
G = F // works
H as integer = F // Does not work (H is "statically" typed)

Using AppGameKit v2 T1 + T2
Systems: Primary: Mac OS X 10.10
Secondary: Windows 7
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 13th Feb 2015 16:35
the compiler is not perfect ...

AGK 108 (B)19 + AppGameKit V2 Alpha .. : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)
unlikely
12
Years of Service
User Offline
Joined: 5th Mar 2012
Location: Ohio, USA
Posted: 13th Feb 2015 16:37
I think this is reasonable... At least the compiler lets you know what's going on and how to fix it.

Using AppGameKit v2 T1 + T2
Systems: Primary: Mac OS X 10.10
Secondary: Windows 7
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 13th Feb 2015 16:58
thats all stumbling blocks, the compiler should understand this,
also because its a basic language.
there is no error in it.

@unlikely
what means "statically" typed ???

AGK 108 (B)19 + AppGameKit V2 Alpha .. : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)
unlikely
12
Years of Service
User Offline
Joined: 5th Mar 2012
Location: Ohio, USA
Posted: 13th Feb 2015 17:05 Edited at: 13th Feb 2015 17:11
Basically:
Static typing: The compiler knows, right off at compile-time, what type the variable is. You specified what it is at declaration, and it cannot change. (C/++, Java, etc.)

Dynamic typing: The compiler doesn't necessarily know what type the variable is until run-time... (Most scripting languages, and others.)

There are more intricacies in the differences, but that's it in a nutshell.

The reason I wrote "statically" typed, is because I don't actually know if the AppGameKit compiler does dynamic typing.

EDIT:
And I realized that the:
F = foo
declaration is actually the same as:
F as integer
F = foo
As I think if you do not use # or $, it assumes integer (so not dynamic at all, and no reason to disallow F as integer = g!)
I think you're right Markus, the compiler should handle this!

Using AppGameKit v2 T1 + T2
Systems: Primary: Mac OS X 10.10
Secondary: Windows 7
Paul Johnston
TGC Developer
22
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 13th Feb 2015 17:20
Quote: "global backgroundicon as integer = icx"


Assigning default values to variables is done at compile time, this allows you to declare the variable in an include file and know that it will have the desired value without the program having to reach the line where you declared it. As such the default value must be a value, rather than a variable, so that the compiler can write it into the bytecode. Then if you declare

backgroundicon will equal 5 from the very first line of your program until you change it.

You can do

but the program would have to reach this line, and only after it was run would your variable equal icx.
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 13th Feb 2015 18:07
Paul's explanation is entirely sensible.

Onwards and sometimes upwards
unlikely
12
Years of Service
User Offline
Joined: 5th Mar 2012
Location: Ohio, USA
Posted: 13th Feb 2015 18:10
Yep, makes sense. Thanks Paul.

What about:



vs.


Why does the first not work and the second work? They are the same, right?

Using AppGameKit v2 T1 + T2
Systems: Primary: Mac OS X 10.10
Secondary: Windows 7
Paul Johnston
TGC Developer
22
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 13th Feb 2015 18:23
Quote: "Why does the first not work and the second work? They are the same, right?"


The first one is a declaration, which is read by the compiler, the second is an assignment which is read at run time.
unlikely
12
Years of Service
User Offline
Joined: 5th Mar 2012
Location: Ohio, USA
Posted: 13th Feb 2015 18:24 Edited at: 13th Feb 2015 18:25
But the second one works if the variable has not been declared, so it is a declaration and assignment, like the first, no?

Using AppGameKit v2 T1 + T2
Systems: Primary: Mac OS X 10.10
Secondary: Windows 7
Paul Johnston
TGC Developer
22
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 13th Feb 2015 18:31
True, but then the variable doesn't have a default value until that line is reached. Whereas the first one declares to the entire program that X is equal to, say, 5 until it is assigned another value.

When you use

without declaring x, the compiler inserts a hidden declaration for you of

to the start of the program
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 13th Feb 2015 18:33 Edited at: 13th Feb 2015 18:34
[EDIT] posted while Paul was posting

The second one is a runtime declaration. It works it out when the program runs. At this point it knows what the value in variable is.

The first - with as integer - tells the compiler to evaluate the variable. Thus, it can only manage values that can be determined before the program is run. The compiler cannot know what the value in variable is.

Quidquid latine dictum sit, altum sonatur
unlikely
12
Years of Service
User Offline
Joined: 5th Mar 2012
Location: Ohio, USA
Posted: 13th Feb 2015 19:09 Edited at: 14th Feb 2015 02:18
Quote: "without declaring x, the compiler inserts a hidden declaration for you of

x as integer = 0

to the start of the program "


Ah, okay. There's the key! Thanks Paul!

(Coming from using Python, with its dynamic typing, this explanation clears things up a lot for me.)

Using AppGameKit v2 T1 + T2
Systems: Primary: Mac OS X 10.10
Secondary: Windows 7
paulrobson
10
Years of Service
User Offline
Joined: 22nd Nov 2014
Location: Norfolk, England
Posted: 13th Feb 2015 20:01
The compiler does not understand constants in the same way that C does. So you cannot write things like:



if you ask it to calculate 2*3+7*9 it will do it, manually. A modern C compiler will say, ah, all those are constants so that is actually 69, AGK2 sees it as a non-constant. When AGK2 says constant it means constant
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 13th Feb 2015 20:23 Edited at: 13th Feb 2015 20:27
this
global backgroundicon as integer = icx
can be intern this or not?
global backgroundicon as integer = 0: backgroundicon = icx
its the same syntax like this we can write
global backgroundicon as integer
backgroundicon = icx
so why the compiler can not? (it shows a message instead translate it)

AGK 108 (B)19 + AppGameKit V2 Alpha .. : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 13th Feb 2015 20:52
Quote: "A modern C compiler will say, ah, all those are constants so that is actually 69, AGK2 sees it as a non-constant"


I used to work with a language called RPG on the IBM midrange systems. We used flags which were '0' or '1', so the program code was always checking for '1'. But whenever you said

IF FLAG *EQ 1

It needed a permanent memory slot for the number 1 for the lifetime of the program. Every reference to '1' was a new variable (even though it was constant!) Frequently we ran out of memory for this reason, especially when stringing together many open programs. So we had to declare a 1 byte variable, set it to 1, and use this variable everywhere in the program. Then it only took up 1 byte for all instances.

Oh how times have changed!

Quidquid latine dictum sit, altum sonatur
Paul Johnston
TGC Developer
22
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 14th Feb 2015 00:55
Quote: "global backgroundicon as integer = 0: backgroundicon = icx"


You can do that but it will be split into two separate lines, so backgroundicon will be 0 from the start of your program until it reaches that line at which point it will change to equal icx.
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 14th Feb 2015 01:17 Edited at: 14th Feb 2015 01:18
i mean the compiler can do this without show a error message.
16 icx as integer = 200
17 global backgroundicon as integer = icx <<< Compilation failed. tables:agc:17 error: Variable default value must be an integer literal or constant

compile / define with default values
icx as integer = 200
global backgroundicon as integer = 0

at row 17 ...
backgroundicon = icx

AGK 108 (B)19 + AppGameKit V2 Alpha .. : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)
Paul Johnston
TGC Developer
22
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 14th Feb 2015 02:06
Technically if the compiler was intelligent enough it could see that icx has a default value of 200 and apply the same to backgroundicon, but unfortunately it isn't that smart and only sees that icx is a variable that could have any value, so doesn't know what value to write to the bytecode.
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 14th Feb 2015 19:36
i think for variables there should be default the value 0 or 0.0 or ""
except constants.
means only at runtime if the app run in the line of code it do the assignment, not before.

AGK 108 (B)19 + AppGameKit V2 Alpha .. : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)

Login to post a reply

Server time is: 2024-11-25 17:49:32
Your offset time is: 2024-11-25 17:49:32