Reflection/Get source: Difference between revisions

J draft
(creation: JavaScript, Python, Ruby)
 
(J draft)
Line 3:
;Task:
The goal is to get the source code or file path and line number where a programming object (e.g. module, class, function, method) is defined.
 
=={{header|J}}==
 
Source code which when executed will recreate the definition can be obtained using <code>5!:5 <'name'</code> where ''name'' is the name of the thing you want source code for. Or, you can use 5!:6 which will provide a "fully parenthesized" variant for the tacit part of any definition.
 
You can also use 4!:4 and 4!:3 to find the file containing the name's definition (if there is one). Line number is not tracked.
 
Examples:
 
<lang J> mean=:+/ %#
5!:5 <'mean'
+/ % #
5!:6 <'mean'
(+/) % #
4!:4 <'mean'
_1
4!:4 <'names'
2
2 { 4!:3 ''
┌────────────────────────────────────────────┐
│/Applications/j64-804/system/main/stdlib.ijs│
└────────────────────────────────────────────┘</lang>
 
We could also provide convenience functions for these mechanisms:
 
<lang J> linrep=: 5!:5@<
srcfile=: (4!:4@<) { a:,~ 4!:3 bind ''</lang>
 
Example use:
 
<lang J> linrep 'names'
list_z_@nl
srcfile 'names'
┌────────────────────────────────────────────┐
│/Applications/j64-804/system/main/stdlib.ijs│
└────────────────────────────────────────────┘
srcfile 'mean'
┌┐
││
└┘</lang>
 
=={{header|JavaScript}}==
6,951

edits