如何让OverloadedStrings语言扩展工作? [英] How do I get the OverloadedStrings language extension working?

查看:198
本文介绍了如何让OverloadedStrings语言扩展工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我启用了重载字符串,但是我无法让它们工作:

I've enabled overloaded strings, but I can't get them to work:

$ cat overloadedstrings.hs 
{-# LANGUAGE OverloadedStrings #-}

import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL

lazy :: BL.ByteString
lazy = "I'm a lazy ByteString"

strict :: B.ByteString
strict = "I'm a strict ByteString"

$ ghci
GHCi, version 6.12.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :l overloadedstrings.hs 
[1 of 1] Compiling Main             ( overloadedstrings.hs, interpreted )

overloadedstrings.hs:7:7:
    No instance for (Data.String.IsString BL.ByteString)
      arising from the literal `"I'm a lazy ByteString"'
                   at overloadedstrings.hs:7:7-29
    Possible fix:
      add an instance declaration for
      (Data.String.IsString BL.ByteString)
    In the expression: "I'm a lazy ByteString"
    In the definition of `lazy': lazy = "I'm a lazy ByteString"

overloadedstrings.hs:10:9:
    No instance for (Data.String.IsString B.ByteString)
      arising from the literal `"I'm a strict ByteString"'
                   at overloadedstrings.hs:10:9-33
    Possible fix:
      add an instance declaration for (Data.String.IsString B.ByteString)
    In the expression: "I'm a strict ByteString"
    In the definition of `strict': strict = "I'm a strict ByteString"
Failed, modules loaded: none.

在哪里可以导入 IsString 实例 ByteString from?这不应该自动发生?我做错了什么?

Where do I import the IsString instances for ByteString from? Shouldn't this happen automatically? What am I doing wrong?

推荐答案

您需要导入 .Char8 模块,比如 Data.ByteString.Char8 Data.ByteString.Lazy.Char8

You need to import the .Char8 modules, such as Data.ByteString.Char8 and Data.ByteString.Lazy.Char8

这篇关于如何让OverloadedStrings语言扩展工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆