我有兩個科學數字,它們是我想要轉換爲Ints的整數,如下所示。默認情況下,使用Scientific時輸入'Double'的限制
請忽略代碼慣例和代碼的慣用性。
> import qualified Data.Vector as V
> import Data.Scientific
> cToTy (Array v) = case V.toList v of
> [String nm, Number p, Number s]
> | all (==True) $ map isInteger [p,s] -- We make sure they're always integers
> , [pr,sc] <- map (forceEitherToInt . floatingOrInteger) [p,s] -- And then hack them out
> forceEitherToInt :: (RealFloat r, Integral i) => Either r i -> Int
> forceEitherToInt (Left _a) = 0 -- This shouldn't happen, so we'll default it to 0 all the time.
> forceEitherToInt (Right a) = fromIntegral a
但是,我得到這個警告,我不知道如何擺脫他們。
JsonUtils.lhs:76:26: Warning:
Defaulting the following constraint(s) to type ‘Double’
(RealFloat r0) arising from a use of ‘forceEitherToInt’
In the first argument of ‘(.)’, namely ‘forceEitherToInt’
In the first argument of ‘map’, namely
‘(forceEitherToInt . floatingOrInteger)’
In a stmt of a pattern guard for
a case alternative:
[pr, sc] <- map (forceEitherToInt . floatingOrInteger) [p, s]
有沒有人有任何想法或想法?
首先,違約(至少在這種情況下)是無害的。 'p'和's'是多態的 - 類似於'RealFloat a => a',但是'forceEitherToInt'的返回類型不包含該類型,所以當'forceEitherToInt'應用時約束必須放棄。爲了讓你的生活更簡單,編譯器會猜測使用哪種類型 - 而「Double」並不是一個錯誤的選擇。如果你真的想擺脫它,給應用程序*或* forceEitherToInt定義一個單形式簽名。 – user2407038 2014-10-28 16:14:56
你需要'forceEitherToInt'來限制'RealFloat'嗎?你沒有使用它,你可以'forceEitherToInt ::(Integral i)=> ri - > Int' – bheklilr 2014-10-28 16:15:22