# include(::Function, ::AbstractString)

**URL:** https://discourse.julialang.org/t/include-function-abstractstring/102624
**Category:** New to Julia
**Tags:** question
**Created:** [August 9, 2023, 9:01am UTC](https://discourse.julialang.org/t/include-function-abstractstring/102624 "2023-08-09T09:01:23Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)
#### Post date: [August 9, 2023, 9:01am UTC](https://discourse.julialang.org/t/include-function-abstractstring/102624/1 "2023-08-09T09:01:23Z")

</div>

Hi,

Can someone provide a simple example of how to use the following function?

```julia
include(::Function, ::AbstractString)

```

Everytime I try to use it returns ERROR: undefVarError : `myfun` not defined.

Thank you

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [August 9, 2023, 9:19am UTC](https://discourse.julialang.org/t/include-function-abstractstring/102624/2 "2023-08-09T09:19:35Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)
#### Post date: [August 9, 2023, 9:29am UTC](https://discourse.julialang.org/t/include-function-abstractstring/102624/3 "2023-08-09T09:29:36Z")

</div>

Do you have an example?

---

<div class="post-metadata">

### Author: ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)
#### Post date: [August 9, 2023, 10:30am UTC](https://discourse.julialang.org/t/include-function-abstractstring/102624/4 "2023-08-09T10:30:34Z")

</div>

The docs are pretty clear, no?

```julia
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.

```

---

<div class="post-metadata">

### Author: ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)
#### Post date: [August 9, 2023, 10:33am UTC](https://discourse.julialang.org/t/include-function-abstractstring/102624/5 "2023-08-09T10:33:15Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)
#### Post date: [August 9, 2023, 10:38am UTC](https://discourse.julialang.org/t/include-function-abstractstring/102624/6 "2023-08-09T10:38:02Z")

</div>

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:

```julia
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

```

---

<div class="post-metadata">

### Author: ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)
#### Post date: [August 9, 2023, 10:51am UTC](https://discourse.julialang.org/t/include-function-abstractstring/102624/7 "2023-08-09T10:51:43Z")

</div>

Thank you. So `include` applies an existing function to code which is inside a julia file? Is that correct?

---

<div class="post-metadata">

### Author: ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)
#### Post date: [August 9, 2023, 10:53am UTC](https://discourse.julialang.org/t/include-function-abstractstring/102624/8 "2023-08-09T10:53:05Z")

</div>

I guess you could say that.  
It gives you a chance to rewrite functions/expressions, before they’re actually evaluated by `include`

---

<div class="post-metadata">

### Author: ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)
#### Post date: [August 9, 2023, 10:53am UTC](https://discourse.julialang.org/t/include-function-abstractstring/102624/9 "2023-08-09T10:53:53Z")

</div>

Understood. Thank you.

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [August 9, 2023, 11:25am UTC](https://discourse.julialang.org/t/include-function-abstractstring/102624/10 "2023-08-09T11:25:35Z")

</div>

> [@sdanisch](#):
>
> `of course only makes sense if you modify/change the expression`

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
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

```
