If you look at the pandoc documentation you see scripts written in Haskell. I have only recently learned the basics of Haskell so I am unfamiliar with some of the idioms that seem to appear in these scripts.
One of the things that I don't understand is the use of def
in these scripts. For example, at the top of Text.Pandoc is the following code:
markdownToRST :: String -> String
markdownToRST =
(writeRST def {writerReferenceLinks = True}) . readMarkdown def
main = getContents >>= putStrLn . markdownToRST
What does the 'def' do after the readMarkdown
and the writeRST
?
In Haskell, def
is just a variable name, nothing special. The def
in this code is Data.Default.def
from the data-default package. It is used to access a default value. The default value it returns depends on the type it is used at. In this case, it returns the default writer options and default reader options, respectively.
To figure such things out, you can use hoogle to search for names or types in the API documentation of Haskell packages. In this case, you can search for +pandoc def to search for the name def
in the pandoc API documentation. The first result is Text.Pandoc.Options.def
. I was confused by the type signature of def
, so I clicked on the Default
type class in the signature which took me to the documentation of the data-default
package.
The documentation of the Default
type class doesn't mention ReaderOptions
, because the data-default package doesn't know about the pandoc package. But in Haskell, type classes are open, so the pandoc package can add the instance Default ReaderOptions
itself. See the list of instance in the documentation of ReaderOptions
. Or see the source code for the actual instance Default ReaderOptions
declaration here.
It comes from the Text.Pandoc.Options
module, which itself is importing it from Data.Default
, where it's defined simply as
class Default a where
def :: a
It's just defining a "default" value for types that implement it, very similar to mempty
from Monoid
but without the restriction that you define mappend
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With