Book Image

Getting Started with Julia

By : Ivo Balbaert
Book Image

Getting Started with Julia

By: Ivo Balbaert

Overview of this book

Table of Contents (19 chapters)
Getting Started with Julia
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
The Rationale for Julia
Index

Reflection capabilities


We saw in this chapter that the code in Julia is represented by expressions that are data structures of type Expr. The structure of a program and its types can therefore be explored programmatically just like any other data. This means that a running program can dynamically discover its own properties, which is called reflection. We already have encountered many of these functions before:

  • typeof and subtypes to query the type hierarchy (refer to Chapter 6, More on Types, Methods, and Modules)

  • methods(f) to see all the methods of a function f (refer to Chapter 3, Functions)

  • names and types: given a type Person:

    type Person
        name:: String
        height::Float64
    end

    Then, names(Person) returns the field names as symbols: 2-element Array{Symbol,1}: :name :height.

    Person.types returns a tuple with the field types (String, Float64).

  • To inspect how a function is represented internally, you can use code_lowered:

      code_lowered(+, (Int, Int))

    This returns the following output...