# Using type alias inside struct

**URL:** <https://discourse.julialang.org/t/using-type-alias-inside-struct/45594>\
**Category:** New to Julia\
**Created:** [August 26, 2020, 8:57pm UTC](https://discourse.julialang.org/t/using-type-alias-inside-struct/45594 "2020-08-26T20:57:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![conditionality](https://avatars.discourse-cdn.com/v4/letter/c/4af34b/32.png) [@conditionality](https://discourse.julialang.org/u/conditionality)\
**Post date:** [August 26, 2020, 8:57pm UTC](https://discourse.julialang.org/t/using-type-alias-inside-struct/45594/1 "2020-08-26T20:57:09Z")

</div>

I have this simple example:

```julia
struct A
    const MyType = Int64
    x::MyType
end

a = A(1)

```

When I run it, I get a complaint about “unsupported `const` declaration on local variable”. Why doesn’t this work? If I lift the type alias outside the struct, it works. But doesn’t that mean I will export MyType to all files that include() this definition?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 26, 2020, 9:34pm UTC](https://discourse.julialang.org/t/using-type-alias-inside-struct/45594/2 "2020-08-26T21:34:12Z")

</div>

You can use a `let` block to make `MyType` only visible to the struct definition:

```julia
julia> let
         MyType = Int64
         
         struct A
           x::MyType
         end
       end

julia> a = A(1)
A(1)

julia> MyType
ERROR: UndefVarError: MyType not defined

```

I’m actually surprised that this works–I would have assumed that a `struct` definition inside a `let` block would not be allowed since it’s not at global scope, but it appears to work just fine.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 27, 2020, 12:53am UTC](https://discourse.julialang.org/t/using-type-alias-inside-struct/45594/3 "2020-08-27T00:53:22Z")

</div>

Maybe what you want is this?

```julia
struct A{ MyType <: Int64 }
   x :: MyType
end

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [August 27, 2020, 1:00am UTC](https://discourse.julialang.org/t/using-type-alias-inside-struct/45594/4 "2020-08-27T01:00:00Z")

</div>

Usually you would put your functions inside a `module`, and you can choose what to `export` from that module.

---

<div class="post-metadata">

**Author:** ![anon37204545](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon37204545](https://discourse.julialang.org/u/anon37204545)\
**Post date:** [August 27, 2020, 10:15pm UTC](https://discourse.julialang.org/t/using-type-alias-inside-struct/45594/5 "2020-08-27T22:15:31Z")

</div>

> [@dpsanders](#):
>
> Usually you would put your functions inside a `module` , and you can choose what to `export` from that module.

And you’d still be able to access `MyType` explicitly with `ModuleName.MyType` if you need it later.
