Why is Yesod.Request.FileInfo (fileContentType) a Text?
Вопрос
I am just starting out with Haskell and Yesod so please forgive if I am missing something obvious.
I am noticing that fileContentType in Yesod.Request.FileInfo is a Text even though Yesod.Content has an explicit ContentType. I'm wondering, why is it not a ContentType instead and what is the cleanest conversion?
Thanks in advance!
Решение
This comes down to a larger issue. A lot of the HTTP spec is stated in terms of ASCII. The question is, how do we represent it. There are essentially three choices:
- Create a special newtype
Ascii
around aByteString
. This is the most correct, but also very tedious since it involves a lot of wrapping/unwrapping. We tried this approach, and got a lot of negative feedback. - Use normal
ByteString
s. This is efficient, and mostly correct, but allows people to enter non-ASCII binary data. - Use
Text
(orString
). This is the most developer-friendly, but allows you to enter non-ASCII character data. It's a bit less efficient than (1) or (2) due to the encoding/decoding overhead.
Overall, we've been moving towards (3) for most operations, especially for things like sessions keys which are internal to Yesod. You could say that ContentType
is an inconsistency and should be changed to Text
, but I think it doesn't seem to bother anyone, is a bit more semantic, and a bit faster.
tl;dr: No good reason :)
Другие советы
You have confused the type of Content
with the type of ContentType
. The fileContent
field of FileInfo
should be ContentType
- and it is, modulo the type alias - the type of fileContentType
is Text
which would be ContentTypeType
. It might help to imaging the last world as a prefixing adjective, so ContentType
== the type of Content
.