Forum
const as string? - Printable Version

+- Forum (https://www.boriel.com/forum)
+-- Forum: Compilers and Computer Languages (https://www.boriel.com/forum/forumdisplay.php?fid=12)
+--- Forum: ZX Basic Compiler (https://www.boriel.com/forum/forumdisplay.php?fid=11)
+---- Forum: Bug Reports (https://www.boriel.com/forum/forumdisplay.php?fid=15)
+---- Thread: const as string? (/showthread.php?tid=199)



const as string? - programandala.net - 04-16-2010

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?


Re: const as string? - programandala.net - 04-16-2010

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"


Re: const as string? - programandala.net - 04-16-2010

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.)


Re: const as string? - britlion - 04-16-2010

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.


Re: const as string? - boriel - 04-16-2010

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.


Re: const as string? - boriel - 04-16-2010

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.


Re: const as string? - programandala.net - 04-17-2010

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.