Why is providing a value for a Const optional

Screenshot of documentation.

I find this confusing. Why would you want to create a constant that has no value?

Is this so during the process of writting your app you can test it without providing any value intending to go back later and provide a value?

Well, I should have just asked ChatGPT without bothering you all.

In Xojo, it’s optional to provide a default value for a constant so that you can:


:white_check_mark:

Use Platform-Specific or Conditional Compilation Values

When you define a constant without an initial value, you’re usually planning to assign different values to it based on build settings, like:

  • Platform (#if TargetMacOS)
  • Architecture (#if Target64Bit)
  • Debug/Release (#if DebugBuild)

Example:

Constant kBaseURL As String

Then, in the Constants pane or Build Settings > Advanced > Constants , you might define:

Constant Name Value Platform
kBaseURL https://dev.example.com Debug
kBaseURL https://prod.example.com Release

:white_check_mark:

Define Project-Level Constants Without Initial Values

In modules or classes, you might define constants as placeholders for subclasses or platform-specific overrides. Leaving them blank avoids committing to a default that may not be valid in all contexts.


:white_check_mark:

Allow Tooling to Inject Values

External build scripts or CI/CD tools may inject values into the constant slots, e.g., version numbers, API keys, or toggles, depending on the environment.


What Happens If You Use It Without a Value?

If you reference a constant that has no value at compile time, Xojo will throw a compiler error. So while it’s allowed to define a constant without a value, it’s not allowed to use it without ensuring it has one under the appropriate condition.


Would you like to see a real-world example involving #if DebugBuild or platform-specific constants?

1 Like

i say this with every dev tool i touch: it would be much MUCH better if the docs were in a wiki.
not a repo, where someone has to accept a PR, but a wiki, where the community runs it.

it was the case, long time ago.

The reality is that while there are lots of people that would add valuable info, there’s also the possibility that a disgruntled customer or other nefarious actor will try to deface the wiki. Even if it requires approvals, the number of edits to review could be very time consuming.

If you have a problem with the docs, file a bug report. They can fix it quickly and out-of-band.

The docs are incorrect, the value is not optional.

Only the data type is optional.

[Insert grumble about AI being wrong and that you shouldn’t ask it questions you aren’t going to validate]

3 Likes

Apparently it’s already fixed:

Sorry Tim. I know you are touchy about this. I should have done more testing.

Reading the AI answer just made me comfortable with the notion that there was some exotic reason that one might not want to provide a value immediately when a Const was declared, but I would never do it myself because my programming style and ability is plain Jane. I failed to determine that the AI answer was wrong.

It is nice that the Documentation has been fixed so quickly.