Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use configurator [closed]

Tags:

haskell

I read the excellent 24 days of hackage. And i would like to give a try to the package configurator

how can i retrieve a list of data like

herlist = [1, "foo", true] ? 
like image 659
jinkou2 jinkou2 Avatar asked Jan 15 '13 15:01

jinkou2 jinkou2


1 Answers

If you have the line

herlist = [1, "foo", true] 

in a configuration file called "example.cfg", then the simplest program to load and show that value would be

{-# LANGUAGE OverloadedStrings #-}

import Data.Configurator
import Data.Configurator.Types (Value)

main = do
    cfg <- load [Required "example.cfg"]

    lst <- require cfg "herlist" :: IO Value

    print lst

The OverloadedStrings extension is used so that we can use ordinary string literals as configuration keys without having to explicitly convert them to Text.

When looking up the key "herlist" we need to explicitly tell the compiler the type we are expecting, because in a simple program like this where we do nothing but print the value, the compiler cannot infer it from context.

The type Value is the raw type for configuration values. In a real program we would usually convert the Value into a custom type used by our program by defining an instance of the Configured type-class.

like image 119
shang Avatar answered Oct 10 '22 05:10

shang