user2138149
user2138149

Reputation: 17266

Variable not defined in Julia docstring

I'm trying to write a docstring for a Julia function.

Here's a cut-down example of what I have written.

"""
    debug(f)

Log the output of `f` at debug level. This function is provided to avoid running
a function which generates data for logging when the log level is not enabled
at debug level.

For example, sometimes it may be useful to log the keys in a dictionary.

# Example
```julia
d = Dict() # do something to add data
key_string = join(keys(d), ", ")
log_message = "'d' contains keys: $(key_string)"
LJ.debug(log_message)
```

However, this performs potentially long running calculations to generate the
log string despite the fact that if the log level is not enabled at debug level
then the generated data is not used.

To avoid this, use an anonymous function (aka lambda function).

# Example
```julia
d = Dict() # do something to add data
lambda = function ()
    key_string = join(keys(d), ", ")
    "'d' contains keys: $(key_string)"
end
LJ.debug(lambda)
```

"""

However, Julia does not seem to like this. When I try to run the module containing this code, Julia complains.

ERROR: LoadError: UndefVarError: `key_string` not defined

I don't fully understand why this is happening.

This is a docstring with a contained code block. So, even if Julia is trying to run the code in the code block to generate the docstring contents, it should still work because key_string is defined.

Upvotes: 0

Views: 59

Answers (0)

Related Questions