我想做类似于我在下面尝试的事情。好像我需要了解更多关于Monads的信息。有什么指针吗?
我确实使用yesod init
开始,yesod add-handler
创建处理程序。
在Handler/Hello.hs
:
getHelloR :: Handler Html
getHelloR = do
let hello = "Hello World"
defaultLayout $ do
$(widgetFile "hello")
在templates/hello.hamlet
:
<h1>Test
<p>#{hello}
运行cabal-dev install && yesod --dev devel
时遇到的错误
是:
Handler/Hello.hs:9:11:
Ambiguous type variable `a0' in the constraints:
(Data.String.IsString a0)
arising from a use of `hello' at Handler/Hello.hs:9:11-28
(blaze-markup-0.5.1.5:Text.Blaze.ToMarkup a0)
arising from a use of `toHtml' at Handler/Hello.hs:9:11-28
Probable fix: add a type signature that fixes these type variable(s)
In the first argument of `toHtml', namely `hello'
In the first argument of `asWidgetT . toWidget', namely
`toHtml hello'
In a stmt of a 'do' block: (asWidgetT . toWidget) (toHtml hello)
答案 0 :(得分:3)
此错误消息的含义是“我知道hello
变量是某种IsString实例,但我不知道哪一个。”换句话说,hello
类型为String
,Text
,Html
或其他类型,但编译器无法根据您提供的信息找出它重新提供。最简单的解决方法是添加一个显式类型签名,例如:
let hello :: String
hello = "Hello World"
这是使用OverloadedStrings
时出现的一个常见问题。