The `eval` is going to do some really odd things there. If you're on a very 
recent 0.5 build you should probably see the following output after doing 
that

```
julia> for name in (:year, :month, :day, :hour, :minute, :second, 
:millisecond)

           func = eval(name)
           @doc """

               $name(dt::TimeType) -> Int64

           Return the $name of a `Date` or `DateTime` as an `Int64`.
           """ -> func(dt::TimeType)
       end
WARNING: replacing docs for 'func :: Tuple{Base.Dates.TimeType}'.
WARNING: replacing docs for 'func :: Tuple{Base.Dates.TimeType}'.
WARNING: replacing docs for 'func :: Tuple{Base.Dates.TimeType}'.
WARNING: replacing docs for 'func :: Tuple{Base.Dates.TimeType}'.
WARNING: replacing docs for 'func :: Tuple{Base.Dates.TimeType}'.
WARNING: replacing docs for 'func :: Tuple{Base.Dates.TimeType}'.
```

where only the last doc, `millisecond`, is stored. What's *really* being 
stored is a doc for `func`:

```julia
julia> keys(Docs.meta(Main))
Any[func]
```

since in the loop you're redefining a global named `func` on each iteration 
and then attaching a doc to that.

Best to avoid `eval` as much as possible and use the double `$` trick for 
this kind of thing.

-- Mike

On Thursday, 3 March 2016 17:17:31 UTC+2, Curtis Vogt wrote:
>
> Thanks Mike, I also managed to come up with an alternative solution:
>
> using Base.Dates
> for name in (:year, :month, :day, :hour, :minute, :second, :millisecond)
>     func = eval(name)
>     @doc """
>         $name(dt::TimeType) -> Int64
>
>     Return the $name of a `Date` or `DateTime` as an `Int64`.
>     """ -> func(dt::TimeType)
> end
>
> Not sure which I like better yet.
>
> On Thursday, March 3, 2016 at 9:05:36 AM UTC-6, Michael Hatherly wrote:
>>
>> Just needs extra `$`s in the docstring, one for expression interpolation 
>> and one for string interpolation:
>>
>> julia> for name in (:year, :month, :day, :hour, :minute, :second, :
>> millisecond)
>>            @eval begin
>>                @doc """
>>                    $($name)(dt::TimeType) -> Int64
>>
>>                Return the $($name) of a `Date` or `DateTime` as an 
>> `Int64`.
>>                """ ->
>>                $name(dt::TimeType)
>>            end
>>        end
>>
>> (`name` will be displayed fully qualified, i.e. `Base.Dates.year`, so you 
>> could convert `name` it to a string to avoid that.)
>>
>> -- Mike
>>
>> On Thursday, 3 March 2016 16:44:38 UTC+2, Curtis Vogt wrote:
>>>
>>> I was hoping to generate several redundant docstrings using code 
>>> generation. Unfortunately I have run into an issue where $name isn't 
>>> being replaced in the docstring:
>>>
>>> julia> using Base.Dates
>>>
>>> julia> for name in (:year, :month, :day, :hour, :minute, :second, :
>>> millisecond)
>>>            @eval begin
>>>                @doc """
>>>                    $name(dt::TimeType) -> Int64
>>>
>>>                Return the $name of a `Date` or `DateTime` as an `Int64`.
>>>                """ ->
>>>                $name(dt::TimeType)
>>>            end
>>>        end
>>> ERROR: UndefVarError: name not defined
>>>  in eval(::Module, ::Any) at ./boot.jl:267
>>>  [inlined code] from ./boot.jl:266
>>>  in anonymous at ./no file:4294967295
>>>  in eval(::Module, ::Any) at ./boot.jl:267
>>>
>>>
>>>
>>> Is there a way I can get this to work?
>>>
>>

Reply via email to