Different types of import by dependency version

I have a module that uses a Control.Exception in Base < 4 , which is a Control.OldException in Base >= 4 . How can I, using cabal or any other tool, get rid of the version dependency (it just depends on Base , not Base < 4 ) and import Control.OldException when using Base >= 4 and Control.Exception when using Base < 4 ?

+4
source share
3 answers

cabal automatically installs specific CPP definitions based on the version of packages used.

So, for your case, I would:

 {-# LANGUAGE CPP #-} module Blah where #if MIN_VERSION_base(0,4,0) import Control.OldException #else import Control.Exception #endif 

This method builds fine with cabal.

(in fact, I would use the new exceptions and would not support a base base <4, but that's just me)

+8
source

At Cabal, this is done using the β€œflags” and its algorithm for resolving constraints. Example (from control-monad-exception in Hackage )

 Flag extensibleExceptions description: Use extensible-exception package default: False (...) if flag(extensibleExceptions) build-depends: extensible-exceptions >= 0.1 && <0.2, base >= 3.0 && <4 else build-depends: base >= 4 && < 5 

On a machine with an old base version, Cabal will try to resolve the dependency using extensibleExceptions False , crash, then try True with it again and use another build-depends , which will succeed. (You can also enable the flag from the command line.)

http://www.haskell.org/cabal/release/cabal-latest/doc/users-guide/authors.html#configurations documents this mechanism, and the rest of the page describes other mechanisms, including direct conditions such as if impl(ghc >= 6.10.0) .

+1
source

This is an agnostic answer in the language, so it may not apply to you.

There are a couple of options

  • Wrap both exceptions in a SuperException that has both implementations. Give it a parameter that tells it which implementation to use based on Base .
  • The Refactor exception must be a child of OldException with excessive calls. (the best way)
-1
source

Source: https://habr.com/ru/post/1346459/


All Articles