eval in function scope (accessing function args)

前端 未结 1 516
遇见更好的自我
遇见更好的自我 2021-01-26 05:03

Given:

abstract ABSGene
type NuGene <: Genetic.ABSGene
     fqnn::ANN
     dcqnn::ANN
     score::Float32
 end

 function mutate_copy{T<:ABSGene}(gene::T)
         


        
1条回答
  •  一向
    一向 (楼主)
    2021-01-26 05:21

    Don't use eval! In almost all cases, unless you really know what you're doing, you shouldn't be using eval. And in this case, eval simply won't work because it operates in the global (or module) scope and doesn't have access to the variables local to the function (like the argument gene).

    While the code you posted isn't quite enough for a minimal working example, I can take a few guesses as to what you want to do here.

    Instead of map(x->("mutate_copy(gene.$x)"),all_fields_except_score), you can dynamically look up the field name:

    map(x->mutate_copy(gene.(x)), all_fields_except_score)
    

    This is a special syntax that may eventually be replaced by getfield(gene, x). Either one will work right now, though.

    And then instead of eval(parse("$(T)("*join(all_fields_except_score,",")*")")), call T directly and "splat" the field values:

    T(all_fields_except_score...)
    

    I think the field order should be stable through all those transforms, but it looks a pretty fragile (you're depending on the score being the last field, and all constructors to have their arguments in the same order as their fields). It looks like you're trying to perform a deepcopy sort of operation, but leaving the score field uninitialized. You could alternatively use Base's deepcopy and then recursively set the scores to zero.

    0 讨论(0)
提交回复
热议问题