Introspection

Introspection refers to code that looks into another bit of code (or itself) to give you more details about it.

Check out REPR

The easiest way to do introspection with Term is to use the Term Repr functionality including functions such as termshow and @showme to get all the details you need about your Julia code. Head over there to find out more.

inspect

You're using a new library, trying to get a hold of the different types they've defined and how they're used. We've all been there, it can take a while and be quite confusing. Term to the rescue!

You can now use inspect(T::DataType) to get all information you possibly need about a type T (and some more). You get: docstring, definition, constructor methods, methods using T and methods using T's supertypes. It can be quite a lot of stuff, so you can use some conveniently placed tags to choose what you want to see:

Term.Introspection.inspectFunction
inspect(T::DataType; documentation::Bool=false, constructors::Bool=true, methods::Bool=true, supertypes::Bool=true)

Inspect a DataType to show info such as docstring, constructors and methods. Flags can be used to choose the level of detail in the information presented:

  • documentation: show docstring with termshow
  • constructors: show T constructors
  • methods: show methods using T in their signature
  • supertypes: show methods using T's supertypes in their signature
source

Try, for example: using Term; inspect(Panel, documentation=true).

typestree

As you know, one of Julia's defining features is its hierarchical types structure. Sometimes, you want to get an overview of this hierarchy but there isn't always a convenient way achieve that... or is there...

import Term: typestree

print(typestree(Float64))
╭────────────── Types hierarchy ───╮
                                  
  Number                          
 ━━━━━━━━                         
                              
    ├── Complex                   
    └── Real                      
        ├── Rational              
        ├── AbstractIrrational    
        ├── AbstractFloat         
        │   ├── Float64           
        │   ├── BigFloat          
        │   ├── Float32           
        │   └── Float16           
        └── Integer               
                                  
╰──────────────────────────────────╯
nothing

Expression & expressiontree

If you're doing a bit of metaprogramming (or teaching it to new Julia coders), you want to get an idea of what the parts of the Expr you're building are. You can use expressiontree to get a Tree based visualization:

import Term: expressiontree
expr = :(2x + √x^y)
expressiontree(expr)
╭────────────────────────────────── 2x+√(x^y) ─────────────────────────────────╮
│                                                                              
                         2x+√(x^y)                                            
                        ━━━━━━━━━━━                                           
                                                                          
                            └── 2x+√(x^y)  (call: +)                          
                                ├── 2x  (call: *)                             
                                │   ├── 2                                     
                                │   └── x                                     
                                └── √(x^y)  (call: )                         
                                    └── x^y  (call: ^)                        
                                        ├── x                                 
                                        └── y                                 
                                                                              
╰────────────────────────────────────────────────────────────────── inspect ───╯

enjoy!