ballerina-platform / ballerina-spec

Ballerina Language and Platform Specifications
Other
167 stars 53 forks source link

First-class support for conditional compilation #1323

Open DaAlbrecht opened 2 days ago

DaAlbrecht commented 2 days ago

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 commented 2 days ago

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 commented 2 days ago

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

sanjiva commented 2 days ago

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 commented 2 days ago

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 commented 2 days ago

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 commented 1 day ago

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 commented 1 day ago

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: