English 中文(简体)
Export the name of the input argument in a function as a string
原标题:
  • 时间:2023-05-24 02:46:52
  •  标签:
  • julia

Following this post, I defined a macro inside a function:

macro Name(arg)
    string(arg)
end

function myfunc(df)
    @Name df
end

tf = 3
myfunc(tf)

What I want is:

  tf  

But what I actually got is

  df  

Is there a way to achieve this?

Thanks

问题回答

The function takes arguments by value (an integer here), so the name tf of the variable passed is not seen by the function body, only 3. Inside the function, df is what the macro sees, not the variable that supplied 3 to the function. You would have to put the macro call in as an argument:

macro Name(arg)
    string(arg)
end

function myfunc(df, name = @Name tf)
    println(df)
    println(name)
    println(@Name df)
end

tf = 3

myfunc(tf)
println()
myfunc(tf, @Name foo)

Result:

3       
tf
df

3
foo
df




相关问题
刚开始和结束的les

我想利用开端和终点拖车来构筑CartesianIndices。 例如,我想在开端=(2,3)和端=(4,5)的情况下建造Cartesian Indices((2,4,3:5)。

Julia symbolic differentiation

Though I ve looked through several pages documenting various differentiation tools in Julia, I have yet to find the following simple functionality. I want to define a function which is differentiable ...

Julia: Return Minimum Date in DataFrame

The question is fairly simple. How do I return the minimum purchase date for each customer using Tidier? using Tidier, DataFrames, Plots, CSV #params f = "path" df = CSV.File(f) |> ...

Reading hex string file in Julia

In python, I do import numpy as np data = np.memmap( mydata.bin , dtype= <i4 , mode= r ) How can I implement this in Julia? I was reading about readbytes and htol but couldn t figure out.

热门标签