Convert GHC.Int.Int64 to Int

I am trying to create a random lazy ByteString the same length as the lazy ByteString that I already have.

So, I take the length of the ByteString and pass it to getEntropy as follows:

 import qualified Data.ByteString.Lazy.Char8 as L import qualified System.Entropy as SE string :: L.ByteString string = L.pack "Hello world!" randomString :: IO L.ByteString randomString = L.fromChunks . (:[]) <$> SE.getEntropy (L.length string) 

(using L.fromChunks . (:[]) to convert from strict ByteString to lazy.)

The problem is that SE.getEntropy is of type Int -> IO ByteString , and L.length is of type L.ByteString -> GHC.Int.Int64 .

How to convert Int64 to Int ?

+5
source share
2 answers

You can turn any type of Integral into another type of Num using fromIntegral :

 fromInt64ToInt :: Int64 -> Int fromInt64ToInt = fromIntegral 
+9
source

fromIntegral

In GHCI:

 let a = 6 :: GHC.Int.Int64 let f :: Int -> Int; fx = x; --this will error fa --this succeeds f (fromIntegral a) 
+4
source

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


All Articles