这些表达式在Julia中表示为LineNumberNode.These expressions are represented as LineNumberNodes in Julia.是否有可能攀登LineNumberNode链以获得文件名而不是nothing?Is there possibly a way to climb up LineNumberNode chain to get a filename and not nothing?也许还有一种方法可以将%__FILE__的计算延迟到运行时,以便我可以在trace中使用该构造?Also maybe there is a way to delay computation of %__FILE__ until runtime, so that I can use that construct in trace?类似的讨论: Julia:创建一个相对于脚本位置的新文件夹和文件 推荐答案引用__source__是Julia手册中建议的内容.这是一个例子Quoting __source__ is what is recommended in the Julia manual. Here is an examplemodule Traceexport @tracemacro trace() return QuoteNode(__source__)endend # module文件f2.jl include("f1.jl")using .Traceprintln("I want: ", (@__FILE__, @__LINE__)); println("I get: ", @trace)x = @tracedump(x)println("This is not what you want: ", PROGRAM_FILE)文件f3.jl include("f2.jl")运行以上现在看一下输出:Running the aboveNow have a look at the output:$ julia f3.jlI want: ("D:\\f2.jl", 5)I get: #= D:\f2.jl:5 =#LineNumberNode line: Int64 7 file: Symbol D:\f2.jlThis is not what you want: f3.jl尤其是: @trace返回具有两个字段的LineNumberNode对象(但我知道这是您想要的)您会看到PROGRAM_FILE为您提供了不同的信息:这是从命令行传递给Julia的文件的名称(因此在我们的情况下为f3.jl,尽管在f2.jl文件中被调用了是f3.jl的include d.)@trace returns you LineNumberNode object that has two fields (but I understand this is what you want)you can see that PROGRAM_FILE gives you a different information: it is a name of the file passed to Julia from the command line (so it is f3.jl in our case, although it was called in f2.jl file which was included by f3.jl).现在更清楚吗? 这篇关于通过外部函数或Julia中的宏获取执行文件的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-28 10:40