Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
const as string?
#1
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?
Reply
#2
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"
Reply
#3
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.)
Reply
#4
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.
Reply
#5
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.
Reply
#6
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.
Reply
#7
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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)