# Scope Issues of Exported Functions in Module

**URL:** https://discourse.julialang.org/t/scope-issues-of-exported-functions-in-module/106784
**Category:** New to Julia
**Tags:** question
**Created:** [November 27, 2023, 6:22am UTC](https://discourse.julialang.org/t/scope-issues-of-exported-functions-in-module/106784 "2023-11-27T06:22:19Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Ian\_Harreschou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ian_harreschou/32/204502_2.png) [@Ian\_Harreschou](https://discourse.julialang.org/u/Ian_Harreschou)
#### Post date: [November 27, 2023, 6:22am UTC](https://discourse.julialang.org/t/scope-issues-of-exported-functions-in-module/106784/1 "2023-11-27T06:22:19Z")

</div>

Probably a basic question and answer I am searching for here, but I am trying to get into package development and it has been quite difficult. This time around, I have made much more progress, but just ran into issues with running `Test` which is baffling me. More than likely comes down to my confusion about scopes and making sure I’ve loaded the proper environments.

I have a module that I want to create, called `FinancialAccounting`. I do an `include("loans.jl")`, and inside `loans.jl` we have a simple function saying hello, and another outputting a number.

In my test file, I have a testset looking for the return of the “hello” function, as well as the number. The `what_is_x` and `hello_package` functions appear, so I think I am loading the module properly. The problem I have, is that when I type into the REPL `FinancialAccounting.hello_package()` it returns the correct phrase, but is `T{Nothing}`. When I just straight up enter `hello_package()` into the same REPL, it returns the proper phrase, and is `T{String}`. This disconnect is causing issues in the test statements and I don’t know how to fix them.

Module File:

```julia
module FinancialAccounting

export hello_package, what_is_x

include("loans.jl")

end

```

Included File:

```julia
function hello_package()
        println("Hello FinancialAccounting!")
end

function what_is_x()
    2
end

```

runtests.jl

```julia
using FinancialAccounting
using Test

@testset "FinancialAccounting.jl" begin
    @test FinancialAccounting.hello_package() == "Hello FinancialAccounting!"
    @test FinancialAccounting.what_is_x() == 2
    @test FinancialAccounting.hello_package() != "Hello World!"
end

```

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [November 27, 2023, 6:43am UTC](https://discourse.julialang.org/t/scope-issues-of-exported-functions-in-module/106784/2 "2023-11-27T06:43:09Z")

</div>

Hello and welcome to the community 👋

The problem might be that the `hello_package` function doesn’t actually return a string, it _prints_ a string. The function `println` does not return anything.

```julia
julia> a = println("hej")
hej

julia> typeof(a)
Nothing

```

so you can never test this function this way

```julia
@test FinancialAccounting.hello_package() == "Hello FinancialAccounting!"

```

You might be confused because a function that returns a string to the REPL will also cause the string to be printed, similar to if the string had been printed using `println`?

What is the `T` you are writing about? I cannot see it appearing in your code.

---

<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: [November 27, 2023, 6:59am UTC](https://discourse.julialang.org/t/scope-issues-of-exported-functions-in-module/106784/3 "2023-11-27T06:59:26Z")

</div>

> [@baggepinnen](#):
>
> You might be confused because a function that returns a string to the REPL will also cause the string to be printed, similar to if the string had been printed using `println`?

It’s printed visibly different, though:

```julia
julia> (() -> "hello")()
"hello"

julia> (() -> println("hello"))()
hello

julia> (() -> begin println("hello"); "world" end)()
hello
"world"

```

> [@Ian\_Harreschou](#):
>
> when I type into the REPL `FinancialAccounting.hello_package()` it returns the correct phrase, but is `T{Nothing}`. When I just straight up enter `hello_package()` into the same REPL, it returns the proper phrase, and is `T{String}`.

Assuming that you’re `typeof`ing the returned value consistently, then this would indicate the two functions are in fact different. Check `FinancialAccounting.hello_package === hello_package`. If `true`, then you evaluated a separate function somewhere else that behaves differently.

---

<div class="post-metadata">

### Author: ![Ian\_Harreschou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ian_harreschou/32/204502_2.png) [@Ian\_Harreschou](https://discourse.julialang.org/u/Ian_Harreschou)
#### Post date: [November 27, 2023, 6:06pm UTC](https://discourse.julialang.org/t/scope-issues-of-exported-functions-in-module/106784/4 "2023-11-27T18:06:28Z")

</div>

`T{String}` was just how I thought the notation was for denoting “type = String”, my mistake. I think it’s something slightly different according to the core/base docs.

I didn’t know that `println` returns a `::Nothing` object, that’s interesting…

---

<div class="post-metadata">

### Author: ![Ian\_Harreschou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ian_harreschou/32/204502_2.png) [@Ian\_Harreschou](https://discourse.julialang.org/u/Ian_Harreschou)
#### Post date: [November 27, 2023, 6:18pm UTC](https://discourse.julialang.org/t/scope-issues-of-exported-functions-in-module/106784/5 "2023-11-27T18:18:12Z")

</div>

Here’s what I tried running, within the `runtests.jl` and after starting a fresh terminal/REPL instance. I’m starting to think this may be an issue/change in the Julia version, because I am following the official walkthrough for package creation and this is where I got all the example tests ([How to develop a Julia package](https://julialang.org/contribute/developing_package/)).

The comparison they use with the `println` and the true `::String` might be incorrect now.

I believe everything in the module is working though. I can access my custom `Loan` struct, as well as the other functions and attributes from `loans.jl`, so I think my exporting is correct. I appreciate the help!

```julia
julia> names(Main)
5-element Vector{Symbol}:
 :Base
 :Core
 :InteractiveUtils
 :Main
 :ans

julia> @ __MODULE__
Main

julia> FinancialAccounting.
Loan amortization amortization_periods eval hello_package
include what_is_x
julia> typeof(FinancialAccounting.hello_package())
Hello FinancialAccounting!
Nothing

julia> typeof(hello_package())
Hello FinancialAccounting!
Nothing

julia> hello_package() === FinancialAccounting.hello_package()
Hello FinancialAccounting!
Hello FinancialAccounting!
true

```

---

<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: [November 27, 2023, 6:34pm UTC](https://discourse.julialang.org/t/scope-issues-of-exported-functions-in-module/106784/6 "2023-11-27T18:34:28Z")

</div>

I doubt it’s a version change, `println` should return `nothing` even 4 years ago. That is either a test doomed to fail or a typo (probably the case).

> [@Ian\_Harreschou](#):
>
> ```julia
> julia> typeof(FinancialAccounting.hello_package())
> Hello FinancialAccounting!
> Nothing
> 
> julia> typeof(hello_package())
> Hello FinancialAccounting!
> Nothing
> 
> julia> hello_package() === FinancialAccounting.hello_package()
> Hello FinancialAccounting!
> Hello FinancialAccounting!
> true
> 
> ```

Good that’s all consistent now.

---

<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: [November 27, 2023, 7:42pm UTC](https://discourse.julialang.org/t/scope-issues-of-exported-functions-in-module/106784/7 "2023-11-27T19:42:17Z")

</div>

@ChrisRackauckas, your turorial needs fixing.

The following tests will fail.

```julia
using YourPackageName
using Test

@testset "YourPackageName.jl" begin
    @test YourPackageName.greet_your_package_name() == "Hello YourPackageName!"
    @test YourPackageName.greet_your_package_name() != "Hello world!"
end

```

With the stated definition.

```julia
function greet_your_package_name()
    println("Hello YourPackageName!")
end

```

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [November 28, 2023, 12:20am UTC](https://discourse.julialang.org/t/scope-issues-of-exported-functions-in-module/106784/8 "2023-11-28T00:20:03Z")

</div>

What tutorial? I’m lost and don’t recall writing an example like this.

---

<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: [November 28, 2023, 12:25am UTC](https://discourse.julialang.org/t/scope-issues-of-exported-functions-in-module/106784/9 "2023-11-28T00:25:00Z")

</div>

Hmm. Maybe this is not actually you:

> **[How to develop a Julia package](https://julialang.org/contribute/developing_package/#step_4_test_your_package)**
>
> The official website for the Julia Language. Julia is a language that is fast, dynamic, easy to use, and open source. Click here to learn more.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [November 28, 2023, 12:44am UTC](https://discourse.julialang.org/t/scope-issues-of-exported-functions-in-module/106784/10 "2023-11-28T00:44:01Z")

</div>

Yeah I don’t know who wrote that, but anyways you’d put a PR to [GitHub - JuliaLang/www.julialang.org: Julia Project website](https://github.com/JuliaLang/www.julialang.org) to get it fixed.
