Posts: 98
Threads: 39
Joined: Apr 2010
Reputation:
0
I got the following error with both 1.2.5 and 1.2.6:
"Initializer expression is not constant"
The line is:
const prompt as string = chr(18,1,62,18,0,8)
I supossed the reason is the chr() calculation, so I put a literal string instead:
const prompt as string = "my prompt"
but nothing changed, the error remains.
The wiki page on CONST is not written, so I searched for CONST in the sources for examples. I searched all the sources for a "const as string" example (using a regular expression), but found nothing.
Are constant strings forbidden?
Posts: 98
Threads: 39
Joined: Apr 2010
Reputation:
0
I guess it has something to do with strings, not with CONST, because the same error happens with DIM in this line:
dim recordPlayerName as string = "Lutero"
Posts: 98
Threads: 39
Joined: Apr 2010
Reputation:
0
Anyway this does what I need:
#define prompt chr(18,1,62,18,0,8)
A compile time macro is not the same thing than a language constant, but it works.
(By the way, I miss a help forum to post doubts. "Wish list", "bug reports" and "how-tos" are not suitable for that.)
Posts: 805
Threads: 135
Joined: Apr 2009
Reputation:
5
You can't put data in a string at the same time as dimming it.
One of my wishlist items is to be able to fill a string array at the dim stage.
So you need to do
dim thing as String
thing="text"
as two statements, for now.
String handling is still a little less sophisticated than numbers; I suspect const doesn't cover them at the moment.
Posts: 1,770
Threads: 55
Joined: Aug 2019
Reputation:
24
programandala.net Wrote:Anyway this does what I need:
#define prompt chr(18,1,62,18,0,8)
A compile time macro is not the same thing than a language constant, but it works.
(By the way, I miss a help forum to post doubts. "Wish list", "bug reports" and "how-tos" are not suitable for that.) This is a very good idea in fact, since chr(18,1,62,18,0,8) will be evaluated in *compile time* (not during run-time) and converted to a string. The compiler does string-folding optimization, so multiple occurrences of the same string in the program will map to the same memory region. This is the best way to do a const string :!:. I can add CONST a$ = ... syntax sugar if you want, but it will be translated into a macro like this.
Posts: 1,770
Threads: 55
Joined: Aug 2019
Reputation:
24
programandala.net Wrote:I guess it has something to do with strings, not with CONST, because the same error happens with DIM in this line:
dim recordPlayerName as string = "Lutero" As britlion said, this is not allowed with strings. The reason for this is that unlike numeric types, strings can change its size during runtime and have a different internal compiler treatment (strings are more like C or Pascal; in fact they're Pascal strings). I will try to workaround this in the future.
Posts: 98
Threads: 39
Joined: Apr 2010
Reputation:
0
boriel Wrote:chr(18,1,62,18,0,8) will be evaluated in *compile time*
That's great. Good to know. I wasn't sure about it.
boriel Wrote:I can add CONST a$ = ... syntax sugar if you want, but it will be translated into a macro like this.
It's not an important issue, but it would be nice, just to make CONST more consistent. Without docs, my intuition made me try CONST with strings.
|