# Create constructor for type without a default constructor

**URL:** https://discourse.julialang.org/t/create-constructor-for-type-without-a-default-constructor/40475
**Category:** General Usage
**Created:** [May 30, 2020, 5:10pm UTC](https://discourse.julialang.org/t/create-constructor-for-type-without-a-default-constructor/40475 "2020-05-30T17:10:23Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![irhum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/irhum/32/25000_2.png) [@irhum](https://discourse.julialang.org/u/irhum)
#### Post date: [May 30, 2020, 5:10pm UTC](https://discourse.julialang.org/t/create-constructor-for-type-without-a-default-constructor/40475/1 "2020-05-30T17:10:23Z")

</div>

Hi there,

So suppose a function like

```julia
function model()
    a=4
    b=2
    x -> a*x+b
end

```

then, `mymodel = model()` seems to be an instantiation of a n implicitly defined struct, and the type itself can be obtained with `typeof(mymodel)`. However, unlike concretely defined types where one can obtain the constructor methods with, say, for a type A with `methods(A)`, when doing `methods(typeof(mymodel))` we get `Type has no methods`, implying these implicit types do not have constructors.

How would one go around defining a constructor for these types, when they do not have a default one?

---

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [May 30, 2020, 5:33pm UTC](https://discourse.julialang.org/t/create-constructor-for-type-without-a-default-constructor/40475/2 "2020-05-30T17:33:17Z")

</div>

What you are returning there is a closure. I am not sure what you mean by “constructor for these types”?

You can also define a struct and derive it from `Function`, this way you can explicitly define the closure and other methods:

```julia
julia> struct Model <: Function
           a
           b
       end

julia> (m::Model)(x) = m.a * x + m.b

julia> mymodel = Model(4, 2)
(::Model) (generic function with 1 method)

julia> mymodel(5)
22

julia> methods(Model)
# 1 method for type constructor:
[1] Model(a, b) in Main at REPL[5]:2

```

---

<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: [May 30, 2020, 6:38pm UTC](https://discourse.julialang.org/t/create-constructor-for-type-without-a-default-constructor/40475/3 "2020-05-30T18:38:07Z")

</div>

> [@irhum](#):
>
> How would one go around defining a constructor for these types, when they do not have a default one?

You’re correct that a closure is implemented as a struct, but I’m not aware of any way to manually construct an instance of that type except through the normal mechanism of creating the closure itself. I think this is by design: closures were not always implemented as structs, and i don’t think the language explicitly promises that they will behave exactly like them.

Once you start wanting more struct-like behavior, I would agree with @tamasgal’s advice to define an actual `struct` and make it callable.

---

<div class="post-metadata">

### Author: ![irhum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/irhum/32/25000_2.png) [@irhum](https://discourse.julialang.org/u/irhum)
#### Post date: [May 30, 2020, 6:56pm UTC](https://discourse.julialang.org/t/create-constructor-for-type-without-a-default-constructor/40475/4 "2020-05-30T18:56:02Z")

</div>

Thanks @tamasgal and @rdeits for clarifying out the difference between a struct and a closure, I can understand they likely have different features by design and that I’d be better off sticking to an actual struct
