[feature-request] Rust-style feature flags?

would it be possible to have, say, @feature("name") and @noFeature("name")?
the usecase would mainly be to avoid dependencies that are only required for uncommonly used features
(… so it would require support on bsconfig’s side too, which isn’t ideal but oh well)

to be fair, there would be… a number of problems with implementing it nicely - the biggest one being that to validate every combination of features, you’d need to compile it 2^numberOfFeatures times

also i’m not sure whether it’s even possible to have, say, two declarations of the same function, e.g.:

// contrived example
type t
@feature("a") let foo: (string) => t = "%identity"
@noFeature("a") let foo: (int) => t = "%identity"

ReScript has conditional compilation based on certain compile-time variables, including environment variables. Pretty sure that can be used to implement feature flags generally speaking. Only works with OCaml syntax though. Here’s an example: https://github.com/yawaramin/dbc/blob/34e27d573539932e1a25aad7f679787e51ffe3d9/src/Yawaramin__Dbc.ml#L1

there are a number of things that are limited to ocaml syntax only right? i wonder if it’s worth switching…

You don’t need to switch fully to one or the other, the same project can contain both syntaxes (one syntax per file).

yeah, but… everything being in rescript syntax except for one single file would bug me so much

so my question is more, are rescript users generally used to reading/writing/working with ocaml files?

Some are, according to ReScript syntax usage poll

small problem… is it possible to set any of the compile time variable in the bsconfig? (because i’d want to turn features on/off when importing, like rust)

i guess completely avoiding (indirect) dependencies is impossible though… even if bsconfig had support for that, package.json definitely doesn’t…

It sounds like you want code splitting with dynamic imports. I have seen this done with nextjs. I haven’t tinkered with doing it without nextjs yet.

1 Like

There used to be a compiler flag -bs-D <variable>=<value> but I’m not sure if that made it across to the rescript command. Certainly -bs-g still works so maybe that will too:
https://rescript-lang.org/docs/manual/latest/build-configuration#bsc-flags
https://rescript-lang.org/docs/manual/latest/build-configuration-schema

I have a single OCaml file in my project to use conditional compilation; it’s a bit awkward but the file doesn’t need to change much so it’s generally fine.

1 Like