# Defining a custom constructor for a type alias?

**URL:** <https://discourse.julialang.org/t/defining-a-custom-constructor-for-a-type-alias/54017>\
**Category:** General Usage\
**Tags:** type\
**Created:** [January 26, 2021, 10:04pm UTC](https://discourse.julialang.org/t/defining-a-custom-constructor-for-a-type-alias/54017 "2021-01-26T22:04:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![maajdl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maajdl/32/22838_2.png) [@maajdl](https://discourse.julialang.org/u/maajdl)\
**Post date:** [January 26, 2021, 10:04pm UTC](https://discourse.julialang.org/t/defining-a-custom-constructor-for-a-type-alias/54017/1 "2021-01-26T22:04:58Z")

</div>

This snippet is invalid:

```julia
boo = Array{Stuff,1}

function boo(x,y,z)
    someStuff = .....
    return someStuff
end

```

It returns this error:

> cannot define function boo; it already has a value

Would you have any idea about defining a custom constructor for a type alias?  
Would it be possible, or doesn’t it make any sense?

Thanks

Michel

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [January 26, 2021, 10:35pm UTC](https://discourse.julialang.org/t/defining-a-custom-constructor-for-a-type-alias/54017/2 "2021-01-26T22:35:36Z")

</div>

You can’t define constructors for aliases. This is because aliases aren’t a new type. They’re just a new name. As such, if you want to define this constructor, you would have to define a constructor for 1 dimensional arrays of `Stuff`. What you probably want is

```julia
struct boo
    stuff::Array{Stuff}
end

```

Then you’re actually making a separate thing which can have it’s own methods.

---

<div class="post-metadata">

**Author:** ![maajdl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maajdl/32/22838_2.png) [@maajdl](https://discourse.julialang.org/u/maajdl)\
**Post date:** [January 27, 2021, 6:32am UTC](https://discourse.julialang.org/t/defining-a-custom-constructor-for-a-type-alias/54017/3 "2021-01-27T06:32:22Z")

</div>

Thanks Oscar  
This is what I did, but this makes the data one step (dot) further away.  
I can’t understand why this is not possible.  
Thanks,  
Michel

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [January 27, 2021, 7:44am UTC](https://discourse.julialang.org/t/defining-a-custom-constructor-for-a-type-alias/54017/4 "2021-01-27T07:44:43Z")

</div>

> [@Oscar\_Smith](#):
>
> You can’t define constructors for aliases

You can, they just need to be `const`, but then they can return anything, e.g. this works:

```julia
struct Stuff end
const boo = Array{Stuff,1}
boo(x,y,z) = "boo"
boo(1,2,3) # returns "boo" 

```

As long as you’re not commiting type piracy by say defining a new constructor for `Array{Int,1}`, I think this is reasonable to use.

---

<div class="post-metadata">

**Author:** ![maajdl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maajdl/32/22838_2.png) [@maajdl](https://discourse.julialang.org/u/maajdl)\
**Post date:** [January 27, 2021, 9:11am UTC](https://discourse.julialang.org/t/defining-a-custom-constructor-for-a-type-alias/54017/5 "2021-01-27T09:11:23Z")

</div>

BAM !!!

So I just had to add a const to the snippet and it works!

```julia
const boo = Array{Stuff,1}

function boo(x,y,z)
    someStuff = .....
    return someStuff
end

```

I like Julia! Looks so logical.  
But in this case, why does the const make all the difference?  
At least a (potentially) “variable” type would make little sense.

Thanks

Michel

---

<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:** [January 27, 2021, 2:11pm UTC](https://discourse.julialang.org/t/defining-a-custom-constructor-for-a-type-alias/54017/6 "2021-01-27T14:11:47Z")

</div>

> [@maajdl](#):
>
> So I just had to add a const to the snippet and it works!

What is the use case you have? I do not understand where that can be useful.

---

<div class="post-metadata">

**Author:** ![maajdl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maajdl/32/22838_2.png) [@maajdl](https://discourse.julialang.org/u/maajdl)\
**Post date:** [January 28, 2021, 9:53am UTC](https://discourse.julialang.org/t/defining-a-custom-constructor-for-a-type-alias/54017/7 "2021-01-28T09:53:09Z")

</div>

Thanks to ask, leandrom, it makes me thing again!

First, I like to use alias, just to improve readability.  
When specifying type in method signature, having a familiar name is more readable than having thing like

`Array{Stuff,1} ` where stuff can also be “complicated”

Second, the default constructor for an alias can be sufficient, but of course it might happen that I need a more specialize constructor. In my last example, it was simply constructing from a dictionary with some selection.

Before I learned that an alias must be `const` , I had to use the solution proposed by Oscar\_Smith which add a layer.

What is your point of view? Would you suggest to avoid using an alias?

Michel

---

<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:** [January 28, 2021, 12:40pm UTC](https://discourse.julialang.org/t/defining-a-custom-constructor-for-a-type-alias/54017/8 "2021-01-28T12:40:11Z")

</div>

I would not be pretentious to “have a point of view” that deserves really to be taken into consideration because I am still learning. Nevertheless, I would mention these points below.

First, I just realized that there are cases where aliases are created with the purpose you have in mind. For example in `StaticArrays`, we have `const SVector{S, T} = SArray{Tuple{S}, T, 1, S}`.

Nevertheless, I was thinking in the following points:

1. Using the alias will make the code less clear to everyone else and, in the future (when you forget why the alias was defined), to you as well. The standard syntax everyone understands. Unless, of course, if the purpose of the package is to provide the new type, such as in the example above.

2. You do not really need an alias just for creating a complicated type. Just write a function with the name you want, for instance. In many cases that will be more clear than the alias, probably.

```julia
julia> struct A
         x 
       end

julia> function initialize_vector_of_A(n) 
         return Vector{A}(undef,n)
       end
initialize_vector_of_A (generic function with 1 method)

julia> a_vec = initialize_vector_of_A(10)
10-element Array{A,1}:

```

1. It is common to use aliases when the types become long, for example with unions `const MyTypes = Union{MyTypeA,MyTypeB,MyTypeC}`, but that is for simplifying the annotations of function dispatch mosty. For example:

```julia
julia> struct A x end

julia> struct B x end

julia> struct C x end

julia> const MyTypes = Union{A,B,C}
Union{A, B, C}

julia> f(a::MyTypes) = a.x
f (generic function with 1 method)

julia> a = A(1);

julia> f(a)
1

julia> f(1)
ERROR: MethodError: no method matching f(::Int64)

```

1. Specialized constructors can be defined as you wish, using functions as I mentioned above, or if you have a new type, like `Stuff`, using new constructors `Stuff(...) = ` or `new(..)` inside the definition of `Stuff`.

2. Just to point that `Vector` is an alias for `Array{T,1}`, which simplifies that the notation a little to `Vector{Stuff}`, and it is important that the meaning of that is very transparent.

---

<div class="post-metadata">

**Author:** ![maajdl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maajdl/32/22838_2.png) [@maajdl](https://discourse.julialang.org/u/maajdl)\
**Post date:** [January 28, 2021, 2:46pm UTC](https://discourse.julialang.org/t/defining-a-custom-constructor-for-a-type-alias/54017/9 "2021-01-28T14:46:51Z")

</div>

Thanks a lot, Leandro.  
For a beginner, like me, such discussions help build more self-confidence, which is as useful as the documentation.  
Michel
