# What is the purpose of the Char type in daml?

**URL:** <https://forum.canton.network/t/what-is-the-purpose-of-the-char-type-in-daml/1104>\
**Category:** App Development\
**Tags:** daml\
**Created:** [August 25, 2020, 5:35pm UTC](https://forum.canton.network/t/what-is-the-purpose-of-the-char-type-in-daml/1104 "2020-08-25T17:35:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris.norris](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/chris.norris/32/688_2.png) [@chris.norris](https://forum.canton.network/u/chris.norris)\
**Post date:** [August 25, 2020, 5:35pm UTC](https://forum.canton.network/t/what-is-the-purpose-of-the-char-type-in-daml/1104/1 "2020-08-25T17:35:17Z")

</div>

In Haskell the string type is defined with `type String = [Char]` and strings can be manipulated using list functions.

In daml it seems there is no Char type and functions such as `splitAt` are implemented using `explode` and `implode` (from the `Text` library) around the prelude list equivalent - incidentally those `Text` functions are undocumented.

So why in daml can I write `'a'` and have the type inferred as `Char`, yet not appear to be able to [do anything with it](https://docs.daml.com/search.html?query=char)?

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [August 25, 2020, 6:05pm UTC](https://forum.canton.network/t/what-is-the-purpose-of-the-char-type-in-daml/1104/2 "2020-08-25T18:05:56Z")

</div>

There isn’t really a `Char` type in DAML. There are some artifacts of a `Char` type that are imposed upon us by virtue of being based on GHC and not having patched it too deeply in this regard. Once you try to actually compile a program with a Char literal, you will get a compilation error in the conversion to DAML-LF either complaining about the literal or the type depending on what the compiler hits first. So unless you are working on the DAML compiler and you need to understand the inner workings of GHC and why we have artifacts of `Char`, you are best off pretending that Char literals result in a parse error and there is no `Char` type.

The more interesting question is why does DAML not have a `Char` type?

This is primarily related to unicode being more complex than one might realize at first. The JVM (the DAML interpreter is implemented in Scala) stores Strings in UTF-16 encoding. the `char` type in Java represents an UTF16 code unit so 2 bytes per char. However, not every unicode character (code point if you want to be precise) can be encoded in 2 bytes in UTF-16. For example, the well-loved `😂` code point is encoded as a so-called surrogate pair consisting of 2 16-bit code points. Java will gladly give you 2 `char`s here. However, for most text-processing usecases you really want to treat this as a single unit (no pun intended, this is 1 code point but 2 code units). There are two solutions out of this mess:

1. Introduce a 32bit `Char` type to represent all possible unicode code points. This is the solution taken by Haskell. The downside here is that it confuses people that are used to the Java model. It also requires a new DAML-LF primitive type which always comes at a cost (not saying that it cannot be justified here).
2. Don’t expose a type for individual characters and instead decompose `Text` into smaller `Text`s in things like `explode`. This clearly doesn’t require a new DAML-LF type and while it might seem like an odd choice it mostly works just fine for the relatively limited text processing that is usually done in DAML.

---

<div class="post-metadata">

**Author:** ![Nick\_Chapman](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/nick_chapman/32/777_2.png) [@Nick\_Chapman](https://forum.canton.network/u/Nick_Chapman)\
**Post date:** [August 26, 2020, 3:49pm UTC](https://forum.canton.network/t/what-is-the-purpose-of-the-char-type-in-daml/1104/3 "2020-08-26T15:49:48Z")

</div>

I would love it it DAML had a distinct types for `Char` and `Text`. This would allow more informative types for some existing primitive functions:

```haskell
explode : Text -> [Char]
implode : [Char] -> Text

```

Other primitives might be changed to work on `Char` instead of on `Text`

```haskell
isNewLine : Char -> Bool
isUpper : Char -> Bool

```

And perhaps we might add an indexing function as primitive (alongside the existing `substring`):

```haskell
index : Int -> Text -> Char

```

---

<div class="post-metadata">

**Author:** ![Luciano](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/luciano/32/319_2.png) [@Luciano](https://forum.canton.network/u/Luciano)\
**Post date:** [August 27, 2020, 7:52am UTC](https://forum.canton.network/t/what-is-the-purpose-of-the-char-type-in-daml/1104/4 "2020-08-27T07:52:47Z")

</div>

Would it be possible to use an existing 32 bit primitive type for this, such as `Int`, to substitute for `Char`?

---

<div class="post-metadata">

**Author:** ![bernhard](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/bernhard/32/356_2.png) [@bernhard](https://forum.canton.network/u/bernhard)\
**Post date:** [August 27, 2020, 7:54am UTC](https://forum.canton.network/t/what-is-the-purpose-of-the-char-type-in-daml/1104/5 "2020-08-27T07:54:32Z")

</div>

That exists: `toCodePoints : Text -> [Int]`, `fromCodePoints: [Int] -> Text`

---

<div class="post-metadata">

**Author:** ![SamirTalwar](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/samirtalwar/32/374_2.png) [@SamirTalwar](https://forum.canton.network/u/SamirTalwar)\
**Post date:** [August 27, 2020, 2:28pm UTC](https://forum.canton.network/t/what-is-the-purpose-of-the-char-type-in-daml/1104/6 "2020-08-27T14:28:46Z")

</div>

For what it’s worth, Rust also tends to use strings where you might expect a character. For example, when calling `char::to_uppercase`, you’ll get an iterator over `char`, not a single one. This is because, for example, “ß” is “SS” in uppercase.

So working with characters isn’t as simple as you might like. For much of DAML’s use cases, `Text` works well and, as @cocreature also points out, means you don’t accidentally split an emoji in half.

(Much of this content shamelessly stolen from [Working with strings in Rust, by fasterthanli.me](https://fasterthanli.me/articles/working-with-strings-in-rust).)
