# Line number of calling site

**URL:** <https://discourse.julialang.org/t/line-number-of-calling-site/1785>\
**Category:** General Usage\
**Tags:** question, juno\
**Created:** [January 30, 2017, 1:36pm UTC](https://discourse.julialang.org/t/line-number-of-calling-site/1785 "2017-01-30T13:36:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TsurHerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tsurherman/32/1234_2.png) [@TsurHerman](https://discourse.julialang.org/u/TsurHerman)\
**Post date:** [January 30, 2017, 1:36pm UTC](https://discourse.julialang.org/t/line-number-of-calling-site/1785/1 "2017-01-30T13:36:10Z")

</div>

Is there a way to get the line number of the calling function?

I would like to encode the filename and the line number of a type.  
so for example if I have

> 

```julia
 type foo
   line::Int32
   fname::String
 end

```

what should be in the constructor so if I have

a = foo() in file “temp.jl” and in line 100 , this information will be present within the instance of foo stored in a.

This is meant as a way to provide better error messages for async tasks.

---

<div class="post-metadata">

**Author:** ![TsurHerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tsurherman/32/1234_2.png) [@TsurHerman](https://discourse.julialang.org/u/TsurHerman)\
**Post date:** [January 30, 2017, 1:37pm UTC](https://discourse.julialang.org/t/line-number-of-calling-site/1785/2 "2017-01-30T13:37:34Z")

</div>

I accept any trickery using stacktraces or backtraces …  
Maybe the Juno guys have an idea?

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [January 30, 2017, 2:19pm UTC](https://discourse.julialang.org/t/line-number-of-calling-site/1785/3 "2017-01-30T14:19:58Z")

</div>

Check out the `backtrace` and `StackTraces.lookup`:

```julia
julia> bt = backtrace()
21-element Array{Ptr{Void},1}:
 Ptr{Void} @0x00007fc2a2cbeb38
 Ptr{Void} @0x00007fc29d1c8c7c
 Ptr{Void} @0x00007fc29d1c8c89
 Ptr{Void} @0x00007fc2a2c7b990
 Ptr{Void} @0x00007fc2a2c9067e
 Ptr{Void} @0x00007fc2a2c8f75e
 Ptr{Void} @0x00007fc2a2c90d29
 Ptr{Void} @0x00007fc2a2c9114f
 Ptr{Void} @0x00007fc2a2caae5d
 Ptr{Void} @0x00007fc2a2c8b27c
 Ptr{Void} @0x00007fc29cf8009a
 Ptr{Void} @0x00007fc29cf800b0
 Ptr{Void} @0x00007fc2a2c7b990
 Ptr{Void} @0x00007fc08d88edb7
 Ptr{Void} @0x00007fc08d88f017
 Ptr{Void} @0x00007fc2a2c7b990
 Ptr{Void} @0x00007fc08d88bbff
 Ptr{Void} @0x00007fc08d88bf10
 Ptr{Void} @0x00007fc2a2c7b990
 Ptr{Void} @0x00007fc2a2c9969f
 Ptr{Void} @0x0000000000000000

julia> StackTraces.lookup.(bt)
21-element Array{Array{StackFrame,1},1}:
 StackFrame[in jl_backtrace_from_here at stackwalk.c:105]                                                              
 StackFrame[in backtrace() at error.jl:26]                                                                             
 StackFrame[in backtrace() at sys.so:?]                                                                                
 StackFrame[in jl_call_method_internal at julia_internal.h:189 [inlined], in jl_apply_generic at gf.c:1945]            
 StackFrame[in do_call at interpreter.c:66]                                                                            
 StackFrame[in eval at interpreter.c:190]                                                                              
 StackFrame[in eval_body at interpreter.c:469]                                                                         
 StackFrame[in jl_interpret_call at interpreter.c:573]                                                                 
 StackFrame[in jl_toplevel_eval_flex at toplevel.c:572 [inlined], in jl_toplevel_eval at toplevel.c:580]               
 StackFrame[in jl_toplevel_eval_in_warn at builtins.c:590]                                                             
 StackFrame[in eval(::Module, ::Any) at boot.jl:234]                                                                   
 StackFrame[in eval(::Module, ::Any) at sys.so:?]                                                                      
 StackFrame[in jl_call_method_internal at julia_internal.h:189 [inlined], in jl_apply_generic at gf.c:1945]            
 StackFrame[in eval_user_input(::Any, ::Base.REPL.REPLBackend) at REPL.jl:64]                                          
 StackFrame[in ip:0x7fc08d88f016]                                                                                      
 StackFrame[in jl_call_method_internal at julia_internal.h:189 [inlined], in jl_apply_generic at gf.c:1945]            
 StackFrame[in macro expansion at REPL.jl:95 [inlined], in (::Base.REPL.##3#4{Base.REPL.REPLBackend})() at event.jl:68]
 StackFrame[in ip:0x7fc08d88bf0f]                                                                                      
 StackFrame[in jl_call_method_internal at julia_internal.h:189 [inlined], in jl_apply_generic at gf.c:1945]            
 StackFrame[in jl_apply at julia.h:1392 [inlined], in start_task at task.c:253]                                        
 StackFrame[in ip:0xffffffffffffffff]                                                                                  

```

---

<div class="post-metadata">

**Author:** ![TsurHerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tsurherman/32/1234_2.png) [@TsurHerman](https://discourse.julialang.org/u/TsurHerman)\
**Post date:** [January 30, 2017, 6:42pm UTC](https://discourse.julialang.org/t/line-number-of-calling-site/1785/4 "2017-01-30T18:42:31Z")

</div>

The stacktrace function works if the calling was done from within some function … then in that case  
stacktrace[2] is the calling function.

However if the call is not wrapped in function then I am left with  
include\_from\_node1(::String) at loading.jl:488  
as the calling file and line…

So I am wondering whether it is possible to infer the calling line and file name even if the code is not wrapped  
in a function … for example in scripts.

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [January 31, 2017, 2:44pm UTC](https://discourse.julialang.org/t/line-number-of-calling-site/1785/5 "2017-01-31T14:44:47Z")

</div>

I’m not sure this is currently possible. You could create a special inner constructor and the caller could use `Foo(line, fname, file=@ __FILE__ , line=@ __LINE__ )`. As long as it’s the caller that’s using `@ __FILE__ ` and `@ __LINE__ ` then I think you’ll get the information you need. Painful, I know.
