# Accessing the constructor of a type as a function

**URL:** <https://discourse.julialang.org/t/accessing-the-constructor-of-a-type-as-a-function/114823>\
**Category:** New to Julia\
**Created:** [May 28, 2024, 9:37am UTC](https://discourse.julialang.org/t/accessing-the-constructor-of-a-type-as-a-function/114823 "2024-05-28T09:37:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [May 28, 2024, 9:37am UTC](https://discourse.julialang.org/t/accessing-the-constructor-of-a-type-as-a-function/114823/1 "2024-05-28T09:37:28Z")

</div>

Is there any way that I can get the construction function of a struct?

What I want to do is the following:

I have a struct Foo and 2 Worker structs:

```julia
struct Foo
  worker_creator::Function
end

mutable struct HardWorker
  data::Integer
  overtime::Integer
  HardWorker(data = 10, overtime = 10) = new(data, overtime)
end

struct LazyWorker
  data::Integer
  slacktime::Integer
  absence::Integer
  LazyWorker(data = 0, slack time = 10, absence = 10) = new(data, slack time, absence)
end
```

I now want to create a Foo struct as follows:

```julia
foo1 = Foo(HardWorker)
foo2 = Foo(LazyWorker)
```

Can this be done? Or do I need to explicitly need to create a creator function?

Thanks in advance,  
Stef

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [May 28, 2024, 11:32am UTC](https://discourse.julialang.org/t/accessing-the-constructor-of-a-type-as-a-function/114823/2 "2024-05-28T11:32:55Z")

</div>

The type _is_ the “constructor function”, however it’s not of type `Function`. Usually, if you want to type-constrain for anything callable, you can use `Base.Callable` which is the same as `Union{Function, Type}` for that reason.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [May 28, 2024, 12:28pm UTC](https://discourse.julialang.org/t/accessing-the-constructor-of-a-type-as-a-function/114823/3 "2024-05-28T12:28:31Z")

</div>

Perhaps this is what you want?

```julia
struct Foo{T}
  worker :: T
end

mutable struct HardWorker
  data::Integer
  overtime::Integer
end

struct LazyWorker
  data::Integer
  slacktime::Integer
  absence::Integer
end

HardWorker(data = 10, overtime = 10) = HardWorker(data, overtime)

LazyWorker(data = 0, slacktime = 10, absence = 10) = 
	LazyWorker(data, slacktime, absence)

julia> Foo(HardWorker())
Foo{HardWorker}(HardWorker(10, 10))

```

---

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [May 28, 2024, 2:51pm UTC](https://discourse.julialang.org/t/accessing-the-constructor-of-a-type-as-a-function/114823/4 "2024-05-28T14:51:55Z")

</div>

Thanks! Although it’s not quite the solution I was looking for, it gave me new insights that will definitely come in handy later on.
