Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First-class support for conditional compilation #1323

Open
DaAlbrecht opened this issue Nov 5, 2024 · 7 comments
Open

First-class support for conditional compilation #1323

DaAlbrecht opened this issue Nov 5, 2024 · 7 comments
Labels
Area/Platform Relates to the Ballerina platform design/usability Design does not work well for some tasks Type/Improvement Enhancement to language design

Comments

@DaAlbrecht
Copy link

Description:

Having conditional compilations in Ballerina (Debug / Release Build) would be pretty helpful or at least setting Debug / Release Flags automatically.

This would allow the user just to check if the Debug flag is set and use this to instrument their applications with extra debug logic without needing to set this flag themselves.

Inspiration from Rust:

cfg: checks https://doc.rust-lang.org/rust-by-example/attribute/cfg.html#cfg

This allows for a nice syntax like

  // using the debug_assertions flag
  if cfg!(debug_assertions) {
      println!("Debugging enabled");
  } else {
      println!("Debugging disabled");
  }

Suggested Labels:
enhancement, langlib, design/usability

@DaAlbrecht
Copy link
Author

I think perhaps a pretty low-hanging fruit would be to just add an extra run command flag bal run -d/r for debug/release (or add it to the existing one bal run --debug <port> and make the port optional) and add it as a default module-level variable.

@jclark jclark added design/usability Design does not work well for some tasks Area/Platform Relates to the Ballerina platform labels Nov 6, 2024
@jclark
Copy link
Collaborator

jclark commented Nov 6, 2024

We already have configurable variables. I would be inclined to handle this by having a module in the standard library that provides a configurable variable that is automatically set to true or false depending on whether it's a debug or release build. WDYT @sameerajayasoma

@jclark jclark changed the title first-class support for conditional compilation First-class support for conditional compilation Nov 6, 2024
@sanjiva
Copy link
Contributor

sanjiva commented Nov 6, 2024

Isn't this the same thing that we discussed earlier where a config value could be statically set (say from a file or a parameter)?

@DaAlbrecht
Copy link
Author

We already have configurable variables. I would be inclined to handle this by having a module in the standard library that provides a configurable variable that is automatically set to true or false depending on whether it's a debug or release build. WDYT @sameerajayasoma

Yea exactly what i mentioned earlier. It wouldn't be conditional builts but is easier to implement and gives already a lot of the usability benefits

@DaAlbrecht
Copy link
Author

DaAlbrecht commented Nov 6, 2024

Isn't this the same thing that we discussed earlier where a config value could be statically set (say from a file or a parameter)?

I think this is already possible: https://ballerina.io/learn/provide-values-to-configurable-variables/

The difference is i would like that ballerina sets this config value automatically without me having to set or unset it.

@sameerajayasoma
Copy link
Contributor

There is a discussion on this topic in Discord initiated by @DaAlbrecht and that thread has more details. https://discord.com/channels/957996897782616114/1301578394944798750/1301578394944798750

@sameerajayasoma
Copy link
Contributor

sameerajayasoma commented Nov 6, 2024

We already have configurable variables. I would be inclined to handle this by having a module in the standard library that provides a configurable variable that is automatically set to true or false depending on whether it's a debug or release build. WDYT @sameerajayasoma

The workaround that doesn't require any changes right now is to define a configurable variable (say DEBUG) and configure it via the command line. I believe @DaAlbrecht is already doing this. I believe this approach is good enough for now.

configurable boolean DEBUG = false;
bal run -- -CDEBUG=true

I am thinking for a bit more first-class conditional compilation solution that would even work for nBallerina as well. Here are some requirements:

  • Different code for different environments
  • Platform-specific code, etc.

@anupama-pathirage anupama-pathirage added the Type/Improvement Enhancement to language design label Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/Platform Relates to the Ballerina platform design/usability Design does not work well for some tasks Type/Improvement Enhancement to language design
Projects
None yet
Development

No branches or pull requests

5 participants