# Re: mutex-flags

```Hi,

I proposed multiway flags about a year ago [1].
With that you could write:```
```
flag xx
values: a, b, c, d

if flag(xx == a)
build-depends: xx-a
if flag(xx == b)
build-depends: xx-b
if flag(xx == c)
build-depends: xx-c
if flag(xx == d)
build-depends: xx-d

The benefits over your proposal, is that there's only one flag!

The alternatives mentioned in the issue [1] consider only automatic flags,
but now as I think about, manual multiway flags can be useful too.

---

For complex "multiway" selection example see:

The idea is that with two flags (transformers-1 and transformers-2) you have
already 4 combinations, so you don't need 4 flags.
That's not intuitive interface for manual flags though.

(There the "elif" syntax would help, which I'll add to Cabal for GHC-8.4)

---

Another way to write "mutex" is (with three flags: a, b, c):

-- at least one:
if !(flag(a) || flag(b) || flag(c))
-- at most one:
if flag(a) && flag(b)
build-depends: base<0
if flag(a) && flag(c)
build-depends: base<0
if flag(b) && flag(c)
build-depends: base<0

That's tedious to go thru all pairs, but quite straightforward.
And can live in a separate block from where the actual flag-logic is
defined.

The same trick can be used with two-flags for three options:

if flag(xx-1)
if flag(xx-2)
-- option 1
else
-- option 2
else
if flag(xx-2)
-- option 3
else
build-depends: base<0

It's also discussed to add explicit

fail: Unsupported configuration

so we don't need to use unsatisfiable "base<0" constraint.

- Oleg

On 19.07.2017 04:50, Lee John wrote:
> I think the mutex flags might be needed. For example, if my program use
> accelerate and I want my program can use both of cuda, llvm, (etc) as the
> backend for accelerating, I need to write a lot of if-statements in cabal
> file to control that there will be only one backend to be used. So I think
> that mutex-flags might be useful.
>
> For example:
>
> …
> flag a
> default: False
> flag b
> default: False
> flag c
> default: False
> flag d
> default: False
> ...
> exec…
> …
> if flag(a) && !flag(b) && !flag(c) && !flag(d)
>       build-depends: xx-a
>
> …
> if !flag(a) && flag(b) && !flag(c) && !flag(d)
>       build-depends: xx-b
>
> …
> if !flag(a) && !flag(b) && flag(c) && !flag(d)
>       build-depends: xx-c
>
> …
> if !flag(a) && !flag(b) && !flag(c)
>       build-depends: xx-d
> …
>
> The above codes are also hard to maintain or extend. Following codes will
> better.
>
> …
> flag a
> default: False
> flag b
> default: False
> flag c
> default: False
> flag d
> default: False
> mutex
> flags: a b c d
> default: d
> …
>
> if flag(a)
> build-depends: xx-a
>
> if flag(b)
> build-depends: xx-b
>
> if flag(c)
> build-depends: xx-c
>
> if flag(d)
> build-depends: xx-d
> ...
> _______________________________________________
> cabal-devel mailing list

```

signature.asc
Description: OpenPGP digital signature

```_______________________________________________
cabal-devel mailing list