Home > Back-end >  A package has been split in two. Is it possible to depend on either the single old one or both the n
A package has been split in two. Is it possible to depend on either the single old one or both the n

Time:10-14

Example: singletons-2 is a large package supporting many types. Those have now largely been moved into the singletons-base package, whereas singletons-3 contains only the central classes. It is easy to write code that works with either singletons-2 or both singletons-3 and singletons-base-3, but singletons-3 alone is not enough and it doesn't make sense to depend on both singletons-2 and singletons-base.

It is possible to declare dependencies conditionally, but only on fixed predicates like compiler version. Is there an alternative way to allow Cabal to resolve to either of the above options, but not to the invalid combinations?

(This is useful specifically for singletons, because version 3 requires a rather new compiler, whereas I don't want to keep depending on the eventually obsolete version 2. What I currently do is to select versions 3 if the compiler supports is, else version 2 – but I'd rather just leave it to Cabal to decide, as it could do using the base bounds.)

CodePudding user response:

You can set a configuration flag that the conditional compilation branches on. Have the flag default to using the newer dependencies. As long as you don't add manual: True to the flag description, cabal will try automatically flipping it to resolve dependencies.

So something like this in the library/executable section:

if flag(singletons3)
    build-depends: whatever
else
    build-depends: whatever other option

Then at the top level in the file:

flag singletons3
    description: Use singletons3
    default: True
    manual: False
  • Related