Is there a function in Haskell that returns the type of its argument (or a string showing the type)?

  • A+

Is there a function one can call within a Haskell program that does what :t does interactively? Or is this something Haskell cannot do since it's type would not make sense. What about a function that returned the name of the type as a String?

typeOf is the canonical way to do this. Beware that it doesn't (can't) handle polymorphism as well as tools outside the language can.

Data.Typeable> typeOf () () Data.Typeable> typeOf "hi" [Char] Data.Typeable> typeOf 3 -- hmmm.... Integer Data.Typeable> typeOf id -- HMMMMMM... <interactive>:4:1: error:     • No instance for (Typeable a0) arising from a use of ‘typeOf’     • In the expression: typeOf id       In an equation for ‘it’: it = typeOf id 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: