Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
array bounds must be constant
#1
How do I create an array with an integer instead of a literal number?

I want to do this:
Code:
DIM personX (numPersons) as ubyte
DIM personY (numPersons) as ubyte
DIM personHealth (numPersons) as ubyte
DIM personWeapon (numPersons) as ubyte

instead of:
Code:
DIM personX (6) as ubyte
DIM personY (6) as ubyte
DIM personHealth (6) as ubyte
DIM personWeapon (6) as ubyte
just in case I decide to change my mind about the number of persons
Reply
#2
Unfortunately this is not (yet) supported. What you're asking is a DYNAMIC ARRAY. Sad This is a feature planned for 1.2.8 (or higher). Dynamic array are a bit trickier, slower and might take a little more memory, but on the other hand are more powerful and allow redimension during runtime. Meanwhile, you can create an array with a MAXPERSONS size (e.g. 20), and use a variable to store the real number of persons:
Code:
CONST MaxPersons As Ubyte  = 20
DIM a(MaxPersons) As Ubyte
LET numPersons = 10 : REM number of persons by now...
FOR i = 1 TO numPersons
    LET a(i) = RND * 10
NEXT i
And yes, you will allocate 10 unused positions.
DYNAMIC Arrays are planned for the next release, please, be patient.
Reply
#3
ah const, I didnt know we had that type thanks Big Grin
Reply
#4
slenkar Wrote:ah const, I didnt know we had that type thanks Big Grin
You can also use #define (like a C preprocessor):
Code:
#define MaxPersons 20
In fact, what's new in 1.2.7 is the complete new preprocessor, which allow powerful macros to be used as inline functions
Code:
#define MyFunc(x, y)  \
    PRINT x * y + 1; \
    PRINT " is the result"

MyFunc(3, 2)
The above program is literally converted to
Code:
PRINT 3 * 2 + 1;
PRINT " is the result"
And even much more complex examples!
Reply
#5
inline functions are cool too Wink
Reply
#6
Hi, Can I use #define functions in fastcalls or subs?
Reply
#7
compiuter Wrote:Hi, Can I use #define functions in fastcalls or subs?
Yes; #define are just MACROS. Wherever you invoke them, they will be *replaced* with its definition.
Reply
#8
macros functions created with #define can contain embebed asm or only basic?
Reply
#9
compiuter Wrote:macros functions created with #define can contain embebed asm or only basic?
Whatever! Of course, if you must use ASM...END ASM to use macros. Remember also to end each line with \ or _ character (line break continue), to tell the compiler those lines are macro ones:
Code:
DIM a As Ubyte = 1

#define MyMacro  \
    PRINT a; " MACRO START" \
    ASM \
    ld hl, _a \
    inc (hl) \
    END ASM \
    PRINT a; " MACRO END"

PRINT "HELLO WORLD"
MyMacro
Notice line continuations "\". You can use "_" instead.
You can think of macros like inline SUBs.

Update: errr... :oops: Just discovered the ASM inside the macro wasn't being correctly supported!. This bug has been fixed and now it works. Please download r2109
Reply
#10
Code:
#define MyFunc(x, y)  \
    PRINT x * y + 1; \
    PRINT " is the result"

MyFunc(3, 2)
I was testing and I can not compile macros with parameters. Sad
Reply
#11
It works ok to me. :?:
Which compiler version are you using? Download 1.2.7 or the latest devel, and try again...
Reply
#12
Perhaps the version. I´ll take a look.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)