Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

haskell - How to define function only for old versions in GHC?

I have a code that uses the fromRight function defined circa GHC 8.2. But I need to downgrade to GHC 8.0.2, which gives an error about Variable not in scope: for fromRight

I was wondering if it possible and how to add the missing definition

fromRight :: b -> Either a b -> b
fromRight _ (Right b) = b
fromRight b _         = b

so that it is only used when I use an GHC version than 8.2.1?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Usually, when you're wondering about a library function, you should use CPP.

{-# language CPP #-}

#if !MIN_VERSION_base (4,10,0)
fromRight :: ...
#endif

The MIN_VERSION_... macros used to be provided by Cabal; now they're provided by GHC. If you want to use them with sufficiently old versions of GHC, you'll need to use Cabal (using either cabal-install or stack).


Before you go to the trouble of doing this, note that there are several packages with names ending in -compat that do all the work for you. In this case, you can use the fromRight from Data.Either.Compat in the base-compat package. Then you don't have to care whether you're using a new enough base library.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...