简体   繁体   English

Haskell,String vs ... String?

[英]Haskell, String vs …String?

I'm currently following the Happstack lite tutorial from their website. 我目前正在关注他们网站上的Happstack lite教程。 http://happstack.com/page/view-page-slug/9/happstack-lite-tutorial http://happstack.com/page/view-page-slug/9/happstack-lite-tutorial

Right now, I'm implementing the echo function, and the compiler is giving me an error message that I don't really understand. 现在,我正在实现echo函数,编译器给我一个我不太懂的错误消息。 Here's my code: 这是我的代码:

echo :: ServerPart Response
echo =
    path $ \(msg :: String) ->
        ok $ template "echo" $ do
            h1 "Echo service"
            p "Giant, Haskell style Papagallo"
            p msg

And here's the error message: 这是错误信息:

src/motiondude.hs:35:15:
    Couldn't match type `[Char]' with `Text.Blaze.Internal.MarkupM ()'
    Expected type: Html
      Actual type: String
    In the first argument of `p', namely `msg'
    In a stmt of a 'do' block: p msg
    In the second argument of `($)', namely
      `do { h1 "Echo service";
            p "Giant, Haskell style Papagallo";
            p msg }'

I thought that a quote-enclosed "thing", like the 我认为这是一个引用封闭的“东西”,就像

"Giant, Haskell style Papagallo"

was a string. 是一个字符串。 Yet, from what I understand from the compiler error, p will not accept a string as argument. 然而,根据我对编译器错误的理解, p不会接受字符串作为参数。 Can someone explain this to me? 谁可以给我解释一下这个?

The problem is you seem to have enabled the OverloadedString extensions which means that 问题是你似乎启用了OverloadedString扩展,这意味着

"foo" :: IsString a => a

To coerce a plain old String to anything implementing IsString just use fromString . 要将一个普通的旧String强制转换为实现IsString任何东西,只需使用fromString In this case however, Happstack has a function html which looks more appropriate. 然而,在这种情况下,Happstack有一个看起来更合适的函数html

大概你有OverloadedStrings ,文字会自动转换为Html

If you remove the type annotation from 'msg', does it work? 如果从'msg'中删除类型注释,它是否有效? That seems to be your problem. 这似乎是你的问题。 On line 6, the string literal gets treated as Html, not as String. 在第6行,字符串文字被视为Html,而不是String。 However, OverloadedStrings only affects string literals, so it won't implicitly convert the msg (which you've said is a String) to an Html. 但是,OverloadedStrings仅影响字符串文字,因此它不会将msg(您所说的是字符串)隐式转换为Html。

I'm not a fan of OverloadedStrings, but that's mainly because of the IsString ByteString instanace... It just bothers me so much more than the Integral Word8 instance, even though they have almost exactly the same problems. 我不是OverloadedStrings的粉丝,但这主要是因为IsString ByteString实例......它只是比Integral Word8实例困扰我,尽管它们几乎完全相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM