我正在编写一个使用UTF-16字符串的应用程序,并尝试为其创建一个IsString
实例,以利用重载字符串扩展功能:
import Data.Word ( Word16 )
import Data.String ( IsString(fromString) )
type String16 = [Word16]
instance IsString [Word16] where
fromString = encodeUTF16
encodeUTF16 :: String -> String16
问题是,当我尝试编译该模块时,GHC 7.0.3会抱怨:
Data/String16.hs:35:10:
Illegal instance declaration for `IsString [Word16]'
(All instance types must be of the form (T a1 ... an)
where a1 ... an are *distinct type variables*,
and each type variable appears at most once in the instance head.
Use -XFlexibleInstances if you want to disable this.)
In the instance declaration for `IsString [Word16]'
如果我注释掉实例声明,它就能成功编译。 为什么会被拒绝呢?对于[Char]的实例看起来很相似,但它可以编译通过。我是不是漏掉了什么?
[Word16]
的新类型包装器,以避免这种问题和冲突。 - ehirdcase s of { Text array offs len -> A.toList array offs len }
并不算太糟糕 :) - ehird