How to get the type of a value in haskell
I'm trying to debug and would like to be able to get a function that returns a value type. Is it possible?
I don't wan't: t, I can't something that can be used in the code
eg:
[showType i | i <- [0..5] ]
returns
[Int,Int,Int,Int,Int]
+3
ditoslav
source
to share
2 answers
The class Typeable
encodes each type uniquely at compile time and can be used for this purpose:
import Data.Typeable
showType :: Typeable a => a -> String
showType = show . typeOf
with the result:
*Main> showType (+)
"Integer -> Integer -> Integer"
*Main> showType [1..5]
"[Integer]"
*Main> map showType [1..5]
["Integer","Integer","Integer","Integer","Integer"]
All that said, Beclilr is right. What you really want may be something else, so more information will help us help you.
+8
Thomas M. DuBuisson
source
to share
Perhaps you want something like TypedHoles
. This allows you to write in _
place of some expression, and the compiler will tell you what type this hole has or should have.
+4
Joachim breitner
source
to share