Hi,
Can someone provide a simple example of how to use the following function?
include(::Function, ::AbstractString)
Everytime I try to use it returns ERROR: undefVarError : myfun
not defined.
Thank you
Hi,
Can someone provide a simple example of how to use the following function?
include(::Function, ::AbstractString)
Everytime I try to use it returns ERROR: undefVarError : myfun
not defined.
Thank you
I’m not sure if it does what you think it does.
The provided function, usually via the do
syntax, will transform the parsed abstract syntax tree loaded from the file.
It is not meant to load a specific function from a file.
Do you have an example?
The docs are pretty clear, no?
include([mapexpr::Function,] path::AbstractString)
...
The optional first argument mapexpr can be used to transform the included code before it is evaluated:
for each parsed expression expr in path, the include function actually evaluates mapexpr(expr). If it is omitted, mapexpr defaults to identity.
Sorry not to me. Maybe for someone who has experience with the language but for someone like me who is not familiar with it, I don’t understand what it means, hence the reason I am asking for a simple example.
Ok, thought you may have just not seen the docstring.
What do you actually want to do then?
Anyways, here is a simple example which should show better what this does:
include("path/to/file.jl") do expr
println(expr)
return expr # of course only makes sense if you modify/change the expression
end
include(println, "path/to/file.jl") # or just that
Thank you. So include
applies an existing function to code which is inside a julia file? Is that correct?
I guess you could say that.
It gives you a chance to rewrite functions/expressions, before they’re actually evaluated by include
Understood. Thank you.
Not necessarily, even if the function doesn’t change the expression, it can run interesting things based on the expression, before it is even eval
ed.
Just for fun, an example of include_string
that runs code from a string instead of a file, and the function mimics the Julia REPL appearance:
julia> include_string(function (expr)
println("another julia> ", expr) # print expression
:(display($expr)) # transform expression to also print its value
end, # expression will be evaled
@__MODULE__, # current module
"""
a, b = 5+1, 5-1
begin
x = 2
y = x^2
end
""") # code to parse to expressions
another julia> (a, b) = (5 + 1, 5 - 1)
(6, 4)
another julia> begin
#= string:4 =#
x = 2
#= string:5 =#
y = x ^ 2
end
4