# "Unsafe" inner constructors of parametric types

**URL:** <https://discourse.julialang.org/t/unsafe-inner-constructors-of-parametric-types/125312>\
**Category:** General Usage\
**Created:** [January 28, 2025, 4:53pm UTC](https://discourse.julialang.org/t/unsafe-inner-constructors-of-parametric-types/125312 "2025-01-28T16:53:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielmatz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielmatz/32/2285_2.png) [@danielmatz](https://discourse.julialang.org/u/danielmatz)\
**Post date:** [January 28, 2025, 4:53pm UTC](https://discourse.julialang.org/t/unsafe-inner-constructors-of-parametric-types/125312/1 "2025-01-28T16:53:43Z")

</div>

I’ve recently learned that you can create functions inside a `struct` definition other than the inner constructor. One use is to create “unsafe” inner constructors, as discussed here: [Bypass inner constructor / call `new` outside inner constructor - #4 by Dan](https://discourse.julialang.org/t/bypass-inner-constructor-call-new-outside-inner-constructor/112142/4)

This seems to work fine when the unsafe inner constructor doesn’t have any type parameters. But using type parameters results in an error:

```julia
julia> struct Foo{T}
       x::T
       global function unsafe_Foo{T}(x) where {T}
       new{T}(x)
       end
       end
ERROR: UndefVarError: `unsafe_Foo` not defined in `Main`
Suggestion: add an appropriate import or assignment. This global was declared but not assigned.
Stacktrace:
 [1] top-level scope
   @ REPL[1]:1

```

Any thoughts on what is going on here? Thanks!

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [January 28, 2025, 4:56pm UTC](https://discourse.julialang.org/t/unsafe-inner-constructors-of-parametric-types/125312/2 "2025-01-28T16:56:35Z")

</div>

You cannot parametrize function names like this. What happens when you define a function for a parametrized type like `Foo{T}` is that you’re not actually defining a function, but adding a method to the _type object_ `Foo{T}`. Since there is no parametrized type `unsafe_Foo`, you cannot add a method to it.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [January 28, 2025, 10:35pm UTC](https://discourse.julialang.org/t/unsafe-inner-constructors-of-parametric-types/125312/3 "2025-01-28T22:35:11Z")

</div>

> [@Sukera](#):
>
> What happens when you define a function for a parametrized type like `Foo{T}`

Relevant section in docs: [Constructors are just callable objects](https://docs.julialang.org/en/v1/manual/constructors/#Constructors-are-just-callable-objects).
