JuliaLang/julia — pull request #2716 by vtjnash (2013-03-31 03:13:31+00:00)
Title: Julia CONFIG dictionary (closes #2430)

I figure the only way to get Jeff to comment on #2430 was to submit the code for it as a pull request. So this creates a CONFIG dictionary for arbitrary configuration parameters.

And assign configuration entries simply:
```julia
CONFIG["answer_color"] = "green"
```

The user can also see the current bindings:
```julia
show(CONFIG)
```
```
Julia Configuration:
OUTPUT_STREAM = TTY(connected,0 bytes waiting)
answer_color = "\e[1m"
gfx/backend = "gtk"
input_color = "\e[1m"
```

The standard usage of this would be to provide default values for any configuration items in the dictionary:
```julia
CONFIG["answer_color", :default] = "blue"
````

Deleting those values resets the value to the default:
```julia
delete!(CONFIG,"answer_color") #CONFIG["answer_color"] is reset to default (blue)
```

Other parameters can be deleted too as follows:
```
delete!(CONFIG,"answer_color", :default, :value, :getter, :setter, :delete, :entry)
```

The CONFIG object supports getter, setter, and delete notification methods. Here's an example from Base that uses a getter method to create compatibility with the old environment variables and converting the input color through the text_colors mapping:
```julia
CONFIG["answer_color", :default, :getter] =
(default_color_answer,
(key,value)->get(text_colors,symbol(get(ENV, "JULIA_ANSWER_COLOR", value)),value))
CONFIG["input_color", :default, :getter] =
(default_color_input,
(key,value)->get(text_colors,symbol(get(ENV, "JULIA_INPUT_COLOR", value)),value))
```

Here's another example from Base at using a setter method to react to changes in the value:
```julia
CONFIG["OUTPUT_STREAM", :default, :setter] =
(STDOUT,
(key,value,hadvalue,newvalue::IO) -> global OUTPUT_STREAM = newvalue)
```
Note that the setter will be called whenever the value is changed, including after a value is deleted (and reverted to default) and right after installing the setter.