作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有以下功能:
import Data.Typeable
import Text.Read (reads)
parse :: (Read b, Typeable b) => String -> IO b
parse msg = case reads msg of
[(value,"")] -> return value
_ -> throwIO $ ErrorCall ("could not parse " ++ msg)
(a,b) <- parse msg :: IO (Int,Int)
s <- parse msg :: IO String
import Data.Typeable
import Text.Read (reads)
parse :: (Read b, Typeable b) => String -> IO b
parse msg = case reads msg of
[(value,"")] -> return value
_ -> throwIO $ ErrorCall ("could not parse " ++ msg ++ " as " ++
show ( typeOf something_that_has_type_b))
import Data.Typeable
import Text.Read (reads)
parse :: (Read b, Typeable b) => b -> String -> IO b
parse dummy msg = case reads msg of
[(value,"")] -> return value
_ -> throwIO $ ErrorCall ("could not parse " ++ msg ++ " as " ++
show ( typeOf dummy))
s <- parse "" msg :: IO String
最佳答案
您不需要虚拟变量,可以使用 ScopedTypeVariables
扩大。
{-# LANGUAGE ScopedTypeVariables #-}
parse :: forall b. (Read b, Typeable b) => String -> IO b
parse msg = case reads msg of
[(value,"")] -> return value
_ -> error $ "could not parse " ++ msg ++ " as " ++
show (typeOf (undefined :: b))
关于Haskell:有没有办法从函数内部推断出函数的返回类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27547824/
我是一名优秀的程序员,十分优秀!