Julia cheat sheet
External cheat sheet
A useful external cheat sheet can be found at The Fast Track to julia 1.0. Below are my own useful notes.
Type hierarchy
To see types above and below
supertype()
subtypetree() # self-defined
To see who exported the object:
parentmodule()
parentmodule(var"@which") # for macros
Macros
| Macro | Parent module | Description |
|---|---|---|
@kwdef |
TODO | TODO |
@forward |
TODO | TODO |
@show |
TODO | TODO |
@time |
TODO | TODO |
@test |
TODO | TODO |
@testset |
TODO | TODO |
@which |
InteractiveUtils |
TODO |
@enum |
TODO | TODO |
@btime |
TODO | TODO |
@code_warntype |
InteractiveUtils |
Warn of potential type instability. |
@inferred |
Test |
Check if returned type differs from inferred return type. |
Type stability
Useful functions for declaring the right types
zero
zeros
one
ones
similar(A)
zeros(axes(A))
eltype
Operator Left operand Right operand Returns Example Type–value relationships :: value (or name) type value / assertion x::Float64 — asserts or annotates isa value type Bool 1.0 isa Float64 → true typeof — value type typeof(1.0) → Float64 Type–type relationships <: type type Bool Float64 <: AbstractFloat → true supertype — type type supertype(Float64) → AbstractFloat subtypes — type Vector{Type} subtypes(AbstractFloat) → [BigFloat, Float16, Float32, Float64]
Are there any other operators that would be good to include in there?
A few come to mind that fit naturally in the same conceptual space:
Type–value relationships:
convert(T, x) — explicitly convert a value to type T. Related to :: but doesn't just assert, it actually transforms.
typeassert(x, T) — the function equivalent of x::T at runtime, throws TypeError if it fails.
Type–type relationships:
>: — the reverse of <:, i.e. "is supertype of". AbstractFloat >: Float64 → true. Less commonly seen but exists.
typeintersect(T, S) — returns the intersection of two types, useful in metaprogramming.
promote_type(T, S) — given two types, returns the type Julia would promote them to in arithmetic. e.g. promote_type(Float32, Float64) → Float64. Very relevant for numerical work.
Introspection:
isabstracttype(T) — returns Bool
isconcretetype(T) — returns Bool. Important distinction in Julia since you can only instantiate concrete types.
fieldnames(T) — returns the field names of a struct type
fieldtypes(T) — returns the types of each field
Design principles
Workflow
module Oscillations
say_hello() = println(“Hello!”)
Your other definitions here
end # module
using .Oscillations osc = Oscillations
then in the file: include(“Oscillations.jl”)