LoginSignup
4
1

More than 5 years have passed since last update.

OverloadedStrings

Posted at

Haskell初心者メモ

OverloadedStrings をつけると文字列リテラルを書いた時に String 型ではなく (IsString a) => a 型になる。整数リテラルを書いた時に Int 型ではなく (Num a) => a 型になるのと似ている。

オプションなしでこういう挙動でない理由を推測するに、文字列に多様な型表現があることが整数リテラルほど自明ではなかったからだろうか。整数リテラルでは32ビット整数、64ビット整数、多倍長整数などがありそうだということがCプログラマにも直感的にわかる。

Haskell 2010 の 2.6 Character and String Literals では

String literals are actually abbreviations for lists of characters

と書かれているので、仕様としては [Char]。それ以上のことは書かれていない(だから拡張してるのだろうけど)。

IsString の instance さえあれば、自分で定義した型でも文字列リテラルとして表現できるのは便利、、かな?
Text, ByteString を扱うときにはとても便利そう。

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1