我正在尝试从页面https://ocharles.org.uk/posts/2014-12-17-overloaded-strings.html了解语言扩展名OverloadedStrings .
启用OverloadedStrings后,将String成为一个类型Data.String.IsString a => a:
Prelude Data.String> :t fromString "Foo"
fromString "Foo" :: IsString a => a
Run Code Online (Sandbox Code Playgroud)
在描述中,作者提到了以下内容:
通过启用此扩展,字符串文字现在是对fromString函数的调用,该函数属于IsString类型类.
这是什么string literals are now a call to the fromString function?
并且作者还提到:
这种多态性非常强大,它允许我们在Haskell源代码中编写嵌入式域特定语言,而不必为其他正常值引入新的构造.
什么without having to introduce new constructs for otherwise normal values意思?
启用OverloadedStrings后,将
String成为一个类型Data.String.IsString a => a
不,这是不正确的.A String仍然是String.它只对字符串文字产生影响,而不是具有类型a的变量,String这些变量仍然可以是Strings.
什么现在字符串文字的通话
fromString功能?
这意味着如果你写一个字符串文字,比如"foo",Haskell隐式写fromString "foo",因此你可以像任何IsString对象一样使用它.
什么不必为其他正常值引入新结构意味着什么?
这意味着我们可以创建自己的类型,我们可以编写某种"迷你解析器",从而在代码中将这些对象写为字符串文字.例如,如果我们创建一个数据类型:
newtype BoolList = BoolList [Bool] deriving Show
Run Code Online (Sandbox Code Playgroud)
然后我们可以编写自己的解析器
instance IsString BoolList where
fromString = BoolList . map toBool
where toBool '1' = True
toBool _ = FalseRun Code Online (Sandbox Code Playgroud)
现在我们可以将Bools 列表定义为:
myboollist :: BoolList
myboollist = "10110010001"
Run Code Online (Sandbox Code Playgroud)
那么我们得到:
Prelude Data.String> myboollist
BoolList [True,False,True,True,False,False,True,False,False,False,True]
Run Code Online (Sandbox Code Playgroud)
因此"10110010001",我们写了一个字符串文字,这意味着我们写道fromString "10110010001".由于类型为myboollistis BoolList,因此可以清楚地解析字符串文字的内容.
因此,如果某些数据类型很复杂,这将非常有用,我们需要大量代码来构造对象.
fromString但是,由于调用被推迟,并且通常不是所有可能的字符串都映射到类型的值(在这种情况下,虽然如果只False为其他所有内容填写是好的,这是有争议的'1'),因此它可能引发错误在运行时,字符串变为"不可解析".
| 归档时间: |
|
| 查看次数: |
132 次 |
| 最近记录: |