(Also posted as a StackOverflow
question:
http://stackoverflow.com/questions/28882241/eval-in-function-scope-accessing-function-args
)
Please try to limit cross-posting unless you're not getting an answer.
Lots of the same people hang out here and at SO - but you can end up
doubling the work and information across the two places. I posted an
answer there.
On Thursday, March 5, 2015 at 10:55:13 AM UTC-5, Phil Tomson wrote:
>
> Given:
>
> abstract ABSGene
> type NuGene <: Genetic.ABSGene
> fqnn::ANN
> dcqnn::ANN
> score::Float32
> end
>
> function mutate_copy{T<:ABSGene}(gene::T)
> all_fields_except_score = filter(x->x != :score, names(T))
> all_fields_except_score = map(x->("mutate_copy(*gene*
> .$x)"),all_fields_except_score)
> eval(parse("$(T)("*join(all_fields_except_score,",")*")"))
> end
>
> ng = NuGene()
>
> mutated_ng = mutate_copy(ng)
>
>
> results in:
>
> ERROR: gene not defined
> in mutate_copy at none:4
>
> If I just look at it as a string (prior to running parse and eval) it
> looks fine:
>
> "NuGene(mutate_copy(gene.fqnn),mutate_copy(gene.dcqnn))"
>
> However, eval doesn't seem to know about gene that has been passed into
> the mutate_copy function.
>
> How do I access the gene argument that's been passed into the mutate copy?
>
> I tried this:
>
> function mutate_copy{T<:ABSGene}(gene::T)
> all_fields_except_score = filter(x->x != :score, names(T))
> all_fields_except_score = map(x->("mutate_copy(*$gene*
> .$x)"),all_fields_except_score)
> #eval(parse("$(T)("*join(all_fields_except_score,",")*")"))
> (("$(T)("*join(all_fields_except_score,",")*")"))
> end
>
> But that expands the gene in the string which is not what I want.
>
>
>
>
>
>
>