TypeApplications
型変数に具体的な型を指定できる
例えば、f :: a -> m cのような多相な関数に対する呼び出し時に
code:hs
f @Int @[] @String 100
のように、順番に個々の型引数への指定、続けて引数、という風に指定できる
aがIntになり、mが[]になり、...という風になる
無意味な指定は型エラーになる
f :: Int -> Intという型引数を持たない関数に指定すると、当然エラーになる
code:hs
f :: Int -> Int
f = undefined
result :: Int
result = f @Int 5 -- error
#wip
型の推論ができずにampbiguous type variable errorになってしまうときに、
簡潔に型アノテーションを書ける
/mrsekut-book-4774192376/397の例
元のコード
型エラーになる
code:hs
main = do
print $ map read "33", "4"
print $ show . read $ "42"
TypeApplicationsを使って簡潔に型アノテーションを書く
code:hs
{-# LANGUAGE TypeApplications #-}
main = do
print $ map (read @Int) "33", "4"
print $ show @Int . read $ "42"
TypeApplicationsを使わないとしたらこう書かないといけない
code:hs
main = print $ (show :: Int -> String) . read $ "42"
https://techblog.asahi-net.co.jp/entry/2022/05/23/162236
序盤にちょろっとだけ
https://qiita.com/mod_poppo/items/478846822828da57fa33
/haskell-shoen/TypeApplications
https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html?highlight=typeapplications#visible-type-application
https://stackoverflow.com/questions/40275080/how-do-you-use-typeapplications-in-haskell
ScopedTypeVariables
https://qiita.com/ruicc/items/3da7572e8234a7708bef
https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/type_applications.html#visible-type-application