Reflection/Get source: Difference between revisions

added Tcl
(Adds Clojure solution)
(added Tcl)
Line 160:
# nil, since Class#nesting is native
</lang>
 
=={{header|Tcl}}==
Tcl's <tt>info</tt> command makes it possible to access the source of nearly anything. This example can show the source code of any proc. The popular <b>tkcon</b> includes a <tt>dump</tt> command which is capable of showing aliases, arrays and more .. and a <tt>edit</tt> command which lets you edit them in an interactive window!
 
<lang Tcl>proc getproc {name} {
set name [uplevel 1 [list namespace which -command $name]]
set args [info args $name]
set args [lmap arg $args { ;# handle default arguments, if it has them!
if {[info default $name $arg default]} {
list $name $default
} else {
return -level 0 $arg
}
}]
set body [info body $name]
list proc $name $args $body
}
 
puts [getproc getproc]</lang>
 
{{out}}
Note the output differs very slightly from the original source: the procedure's name is fully namespace-qualified, and the arguments are in "canonical list form", which does not include braces in this simple case.
 
<lang Tcl>proc ::getproc name {
set name [uplevel 1 [list namespace which -command $name]]
set args [info args $name]
set args [lmap arg $args { ;# handle default arguments, if it has them!
if {[info default $name $arg default]} {
list $name $default
} else {
return -level 0 $arg
}
}]
set body [info body $name]
list proc $name $args $body
}</lang>
 
=={{header|zkl}}==
Anonymous user