ulfhorst
@ulfhorst has joined the channel
fmt.Printf("%T", "hello") // string Data.Typeable を import して, showType = show . typeOf という関数を呼び出せば文字列が得られます.これを出力することで可能です(一部この方法でできないものもありますが,大抵は大丈夫だと思います)>>> import Data.Typeable >>> showType = show . typeOf >>> putStrLn $ showType $ Just 1 Maybe Integer
(Just () :: _) <> Nothing というように :: _ という表記を記載すると型情報をコンパイル時にエラーメッセージとして表示してくれます.GHCi 上でも可能で
>>> (Just () :: _) <> Nothing
(Just () :: _) <> Nothing
<interactive>:8:14: error:
• Found type wildcard '_' standing for 'Maybe ()'
To use the inferred type, enable PartialTypeSignatures
• In an expression type signature: _
In the first argument of '(<>)', namely '(Just () :: _)'
In the expression: (Just () :: _) <> Nothing
• Relevant bindings include
it :: Maybe () (bound at <interactive>:8:2)
Maybe () であることを教えてくれます)f :: a -> String f x = show $ typeOf x
>>> import GHC.Generics >>> putStrLn $ datatypeName $ from $ Just 1 Maybe
{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics
data A = A deriving Generic
Typeable の手法と同じく完全な型変数になっているf :: a -> String f x = datatypeName $ from x
f :: a -> String f x = datatypeName $ from $ Just x