使用重载的字符串 [英] Using Overloaded Strings

查看:127
本文介绍了使用重载的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OverloadedStrings扩展确实非常有用,但是有一些缺点. 考虑以下函数定义:

OverloadedStrings extension is really very useful, however it has some downsides. Consider the following function definition:

someFunction :: ToJSSTring a => a -> IO ()
someFunction = js_function . toJSSTring

在这种情况下,如果要传递文字值,则必须在启用OverloadedStrings时显式添加类型签名:

In this case when if I want to pass a literal value I have to add a type signature explicitly when OverloadedStrings is enabled:

someFunction ("This is plain string" :: String)
someFunction ("And this one is Text" :: Data.Text.Text)

这种必要性的原因很明显,我想OverloadedStrings旨在简化将文字值传递给具有严格类型签名的函数,从而使开发人员无需在任何Text处编写pack即可值是必需的.

The reason for this necessity is quite obvious, I suppose OverloadedStrings was designed to ease the passing of literal values to functions that have strict type signatures, making the developer free from writing packs everywhere where a Text value is needed.

问题是,有没有办法将所有没有类型签名的所有字符串文字默认为TextString?还是应该将我的代码拆分为通用函数(具有ToJSString类型约束)和任意函数,这些函数的参数具有严格的类型签名?

The question is there any way, say, to default all string literals without type signatures to Text, or String? Or should I just split my code to general functions (with the ToJSString type constraint) and arbitrary ones, which have strict type signatures for their arguments?

推荐答案

您也可以打开ExtendedDefaultRules(

编辑.您可能还想将default (Text)添加到模块顶部,以使其默认使用Text而不是String.

EDIT You may also want to add default (Text) to the top of your module to have it use Text instead of String by default.

这篇关于使用重载的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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