Language Bug regarding Const specifiers
BlitzMax Forums/BlitzMax Programming/Language Bug regarding Const specifiers
| ||
Is there any legitimate reason why the language doesn't allow the following declaration for TestLength?Strict Const TestString:String = "a constant string" Const TestLength:Int = TestString.length The error is that constant initializers must be constant. However, since TestString is const, TestString.length is also going to be constant of course. I didn't file this as a bug report because there's probably some legitimate reason that I just don't know about. |
| ||
Well I "think" TestString.length will be evaluated at runtime and not at compilation time, the way to do this would be to have pre-compiler directives. |
| ||
Things get complicated when your String looks like : "~qA nice quoted string...~nwith line breaks...~q" Of course, I suppose you just need a very clever pre-compiler... |
| ||
yeah you couldn't use Len(TestString) either because it required code to be run. Consts are defined at compile time and stored in the exe. You'll have to make it a global and set it in code in some initialisation function. |
| ||
Well, it's not like it couldn't be done. The compiler already handles strings at compile-time, so it could very well compute the length at compile time too. It's one of the cases where "it's just done like that", but it's not inherently impossible. |
| ||
Apart from the reasons above - why would you want to have a string stored in one constant, and its length in another? The whole point of OOP is that you don't have to do things like this. |
| ||
Well, it seems silly I suppose, but the primary reason was just for readability. The constant string I'm using is the opening tag for a custom text specifier, and the code then searches for a specific character in the string at the textspecifier.length index of the string. While I realized I could just use TextSpecifer.length in it's place, I thought using a constant that was set to the length of the text specifier would be more readable, and would automatically update if I changed the text specifier string. Not really needed (and the workaround is simple) but I just stumbled across it and thought I'd ask. |
| ||
I think I sort of agree with you. In OOP should I need to know what methods the string types have? (encapsulation or something) . If that is the case you should probably use the len(stringvar) function. If you had a const string variable and were going to refer to it's length without it changing then it seems alright to put that length in a const. It would help readability and save some function calls. . On the other hand... not that fussed about it either. |