Can we have an #IfDef please?

After many years coding in C, I sorely miss conditional compiling as provided by #Define and #IfDef#Else#EndIf. This is very useful when producing two versions of a program. For example, suppose I have two versions of my masterpiece, one free and another a “pro” version with more features for which I want to be paid. Without #IfDef I would have to produce two versions of the code. If I then want to add/change something or correct a bug, I would have to maintain both versions, a recipe for trouble. I’d like to be able to include it all in one version of the code with something like:

#Define PRO 1

#IfDef PRO
// Code for the “pro” version only
#EndIf
//Code common to both versions

With this I could compile the “pro” version then delete or comment out the #Define to compile the free version.

1 Like

You can accomplish this by defining a constant:

Const PRO = True
#If PRO Then

#Else

#EndIf
6 Likes

Well I’ll be… There’s no reference to #If in the documentation so I didn’t know that a Const can be used in this way. Many thanks Andrew!

Check this link:
https://documentation.xojo.com/api/compiler_directives/if...endif.html

I hate the new documentation, FWIW. I had to search a bit for it. AlbertoD beat me to the link.

6 Likes

See also the issues here:

Request: #IF detection of Modules
and
Add function to test for identifier existants

Old docs, second option:
image

New documentation, I don’t see it:

Edit: I scrolled down and I don’t see the # if… # Endif page, just #if as reserved word (new documentation).

1 Like

You have to scroll down to get to it.

1 Like

I use App.Constants like this to set whether it is an AppStore App (or not). When I build using XojoScript, I can revise this constant many times for multiple Builds, so I don’t need to build manually.

I have a IDE Communicator and a global. I just need to adjust the version in the build script which can make one of 4 different versions of my app with helper app and everything else.

It would be great if you could include or exclude whole classes or windows with #if PRO. For example, you could exclude certain windows for a light or store version.

Today only code sections can be set with #If PRO.

Is there any interest for a feature request that besides targets also a custom constant is supported to include whole classes for specific compilation?

1 Like

I think about something like this:

1 Like

If you exclude all of the things that reference an item in your code using constants, the compiler should do that work for you.

1 Like

I see! Thank you for your reply.
If the compiler already takes care of this, then there is no need for the additional condition.

Perhaps, but these flags would help the compiler catch a mistake. Without the flags, if you accidentally reference an object you wanted excluded, well it gets included. With such a flag, the compiler would complain it doesn’t exist.

What’s a little frustrating is this is purely an IDE limitation. Wether or not an item is included is literally written as an #if statement. Those checkboxes essentially just build the statement for you. So including or excluding things based on platform or other constants could be done if the IDE would support it.

1 Like

Well you can exclude items by turning off their platform flags on the advanced tab of the properties palette. Turning off 32 & 64 bit, for the project type you’re using does make the item get excluded.

Which shows we are really close of having the possibility to exclude items based on constants; probably trivial to add to the IDE.

…and we’re back to the armchair engineering. I’m out.

1 Like

No, I didn’t say trivial.

Maybe “trivial” wasn’t the best wording. I’m just saying it “looks” close.