On Mon, Jul 27, 2015 at 6:02 PM, Christoph Ortner
<christophortn...@gmail.com> wrote:
> Thank you, both.  Combined with your suggestions and some more fiddling I
> got it to work; see below. This doesn't use all the ideas you suggested, so
> it probably not the most elegant solution. I'll try to understand your other
> suggestions and incorporate them. Many thanks again,
>   Christoph
>
>
>
> module Test
>
> using Docile, Lexicon, Compat
>
> export ffun
>
> macro protofun(fname, argtypes...)
>     docstr = argtypes[end]
>     argtypes = argtypes[1:end-1]
>     str = "@doc doc\"$(docstr)\"->\nfunction $(fname)("
>     for n = 1:length(argtypes)
>         str *= string("arg", n, "::", argtypes[n], ", ")
>     end
>     str = str[1:end-2]
>     str *= ") \n"
>     str *= "    error(string(\"AtomsInterface: `$(string(fname))(\", "
>     for n = 1:length(argtypes)
>         str *= string("\"::\", typeof(arg", n, ")")
>         if n < length(argtypes)
>             str *= ", \", \", "
>         end
>     end
>     println(str)
>     eval(parse(str))

Don't call `eval` and don't use `parse`....

`eval`: You will be evaluating it in the wrong scope (the module the
macro is defined)
`parse`: operate on the AST is much safer.

> end
>
> @protofun(ffun, Number, String, "yads")
>
> end
>

Reply via email to