# How to check if variable is a typed global?

**URL:** <https://discourse.julialang.org/t/how-to-check-if-variable-is-a-typed-global/103172>\
**Category:** General Usage\
**Created:** [August 24, 2023, 9:35pm UTC](https://discourse.julialang.org/t/how-to-check-if-variable-is-a-typed-global/103172 "2023-08-24T21:35:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)\
**Post date:** [August 24, 2023, 9:35pm UTC](https://discourse.julialang.org/t/how-to-check-if-variable-is-a-typed-global/103172/1 "2023-08-24T21:35:00Z")

</div>

(other then `try` to assign a wrong-typed value and `catch` the error)

---

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [August 24, 2023, 10:13pm UTC](https://discourse.julialang.org/t/how-to-check-if-variable-is-a-typed-global/103172/2 "2023-08-24T22:13:24Z")

</div>

Let’s say you want to check `myvar` - you assume it is defined in `Main` and has type `Int64`.

```julia
myvar::Int64 = 1

function checkglobal()
    :myvar in names(Main) && # or hasproperty(Main, :myvar)
    Base.return_types(() -> getproperty(Main, :myvar))[1] == Int64
end

#true
println(checkglobal())

```

Another option (this only checks for _global typed_ quality - without doing an explicit type test):

```julia
using Test
@inferred (() -> Main.:myvar)()

# or

@inferred (() -> getproperty(Main, :myvar))()

```

The above will fail if `myvar` is not a global typed defined in `Main` module.

Not sure how you want to treat the `const` scenario (you can have a _typed global_, which is also declared as `const`). However, you can add the following on the mix - depending on your end-goal:

```julia
# false - in the above snippet context
isconst(Main, :myvar)

```

---

<div class="post-metadata">

**Author:** ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)\
**Post date:** [August 25, 2023, 9:21pm UTC](https://discourse.julialang.org/t/how-to-check-if-variable-is-a-typed-global/103172/3 "2023-08-25T21:21:32Z")

</div>

@algunion thank you.

Do I understand it correctly that Base.return\_types is not in the public API?

Also, I have tried to modify your function as to accept the variable name as argument - but it doesn’t work. Why?

```julia
myvar::Int64 = 1

function checkglobal(x)
    x in names(Main) && 
    Base.return_types(v -> getproperty(Main, v))[1] == Int64
end

# false - why?
checkglobal(:myvar)

```

---

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [August 25, 2023, 9:43pm UTC](https://discourse.julialang.org/t/how-to-check-if-variable-is-a-typed-global/103172/4 "2023-08-25T21:43:47Z")

</div>

I can see the `return_types` used in the manual [here](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#man-typed-globals).

I don’t know why it doesn’t appear when searching the docs. The function is not used much in the Julia codebase but is well-represented (and useful) in the tests.

It seems to me that it is treated as being part of the public API, but somehow it doesn’t show (or is not properly exported in the docs). I’ll look into it.

As for your question related to why doesn’t work when you use the variable as an argument - I don’t have an answer right away, I’ll need to test/check that and come back with an answer later.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [August 26, 2023, 8:02pm UTC](https://discourse.julialang.org/t/how-to-check-if-variable-is-a-typed-global/103172/5 "2023-08-26T20:02:21Z")

</div>

You don’t need the workaround via `return_types` here, there’s an internal function `Core.get_binding_type(:: Module, ::Symbol)` that should do what you want

---

<div class="post-metadata">

**Author:** ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)\
**Post date:** [August 26, 2023, 8:18pm UTC](https://discourse.julialang.org/t/how-to-check-if-variable-is-a-typed-global/103172/6 "2023-08-26T20:18:58Z")

</div>

@simeonschaub thank you, it works.

But shouldn’t be there some public API way to get this information, similar to `isconst`?
