What is the reason that there is a separate Char type for single letter Strings?
Ouch, that is a terrible explanation and I'm sorry you had to read that.
So first of all, Char
is a module, not a type. The type is called char
. This might seem pedantic, but people coming from object-oriented languages often get confused by modules designed specifically for a type since they appear very object-like, and might seem like they should be objects since it feels so natural and convenient coming from OO. But it's not, they shouldn't, and it certainly doesn't help to have badly written documentation reinforce such flawed conceptions.
Anyway, to the actual question: why does the char
type exist separately from the string
type? Because char
's are what string
s are made of.. It's not a "single letter string", it's the letter itself, or rather a character (which is of course where the name ´char´ comes from). A string is essentially just an array
of char
s.
Without the char
type you wouldn't be able to manipulate the elements of a string
directly. You wouldn't even be able to compare one string to another, unless string comparison is hard-coded into the language. Which is a non-trivial problem with hard and non-obvious trade-offs, and really just moves the problem to where it's harder to deal with.
Even if you do have hard-coded string comparison and can use single-letter strings in place of char
s, you now have no type level guarantee that a string contains only a single letter and would therefore have to add runtime checks to make sure the string is not empty or longer than a single letter, and decide what to do if it is. All those checks will quickly add up to a significant performance cost, and the hidden decisions add up to a significant cognitive burden.
Alternatively you could of course just do what dynamic languages like JavaScript and Python do, which is to crash or behave unexpectedly when the planets don't line up right. But I assume you're interested in Reason because you're tired of that.