I'm trying to debug and would like to be able to get a function which returns a type of a value. Is such a thing possible?
I dont wan't :t, I wan't something which can be used in code
such as:
[showType i | i <- [0..5] ]
returns
[Int,Int,Int,Int,Int]
The Typeable class 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 a result of:
*Main> showType (+)
"Integer -> Integer -> Integer"
*Main> showType [1..5]
"[Integer]"
*Main> map showType [1..5]
["Integer","Integer","Integer","Integer","Integer"]
All this said, Bheklilr is right. What you actually want might be something different so more details would help us help you.
Maybe you rather want something like TypedHoles. It allows you to write _ instead of some expression, and the compiler will tell you what type this “hole” has, or should have.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With