samlerepo/2019/Julia/Code/16.Macro.jl

29 lines
920 B
Julia
Raw Normal View History

2019-09-12 16:32:16 +02:00
# Julia comes with a lot of useful macros. Youv'e already seen the @show macro.
x = 5
@show x
# Macros are a small part of julias huge metaprogramming system.
# There exists a bunch of useful macros, both in julia itself, and in
# different packages. A macro is basically a function that takes in a list
# of arguments and generates a code block at compiletime. It is possible
# to create your own macros, but that takes some reading up on the
# metaprogramming section in the julia docs.
# Probably one of my most used macros is the @time macro
reallyslowfunction() = sleep(2)
@time reallyslowfunction()
# If you want to time a whole code block this is done with a "begin" block
@time begin
# Slow code
sleep(1)
end
2019-09-12 16:48:06 +02:00
# The @inbounds macro removes all checks to see if you try to acces elements
# outside any container, so might be faster, but definately more unsafe.
a = [1, 2, 3]
@inbounds println(a[2])
2019-09-12 16:32:16 +02:00