# Basic question about type alias

**URL:** <https://discourse.julialang.org/t/basic-question-about-type-alias/130844>\
**Category:** General Usage\
**Tags:** question, type, alias\
**Created:** [July 18, 2025, 11:16am UTC](https://discourse.julialang.org/t/basic-question-about-type-alias/130844 "2025-07-18T11:16:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nikos\_Gianniotis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nikos_gianniotis/32/11487_2.png) [@Nikos\_Gianniotis](https://discourse.julialang.org/u/Nikos_Gianniotis)\
**Post date:** [July 18, 2025, 11:16am UTC](https://discourse.julialang.org/t/basic-question-about-type-alias/130844/1 "2025-07-18T11:16:22Z")

</div>

For the first time, I defined my own type alias and was surprised by the following behaviour:

```julia
const VecScalar{T<:Number} = Tuple{Vector{T}, T}

x::VecScalar{Float64} = ([1.0, 2.0], 3.0) # works as expected.

x::VecScalar{Float64} = ([1, 2], 3.0) # was expecting to fail, but it works. Why?

```

I am surprised because I expected that all the elements in this alias type must be of the same `T`. Evidently, I am ignoring something very basic, so please bear with my ignorance.

How do I enforce that the elements in the vector are of the same type as the second element in tuple?

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [July 18, 2025, 11:23am UTC](https://discourse.julialang.org/t/basic-question-about-type-alias/130844/2 "2025-07-18T11:23:19Z")

</div>

So you want to avoid type conversion from `Int` to `Float64`? You mean you want to type-annotate the right hand side of the equation instead of the left hand side?

```julia
julia> ([1, 2], 3.0)::VecScalar
ERROR: TypeError: in typeassert, expected Tuple{Vector{T}, T} where T<:Number, got a value of type Tuple{Vector{Int64}, Float64}
Stacktrace:
 [1] top-level scope
   @ REPL[3]:1

```

---

<div class="post-metadata">

**Author:** ![Nikos\_Gianniotis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nikos_gianniotis/32/11487_2.png) [@Nikos\_Gianniotis](https://discourse.julialang.org/u/Nikos_Gianniotis)\
**Post date:** [July 18, 2025, 11:27am UTC](https://discourse.julialang.org/t/basic-question-about-type-alias/130844/3 "2025-07-18T11:27:43Z")

</div>

Sorry, I wasn’t clear enough. I thought my alias type definition would make sure that

```julia
x::VecScalar{Float64} = ([1, 2], 3.0)

```

fails, because the first element of the tuple, the vector, has elements of type `Int` while the second element of the tuple has type `Float64`. Clearly, my alias type does not enforce this.

How dow I have to modify my alias type in order to enforce this?

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [July 18, 2025, 11:29am UTC](https://discourse.julialang.org/t/basic-question-about-type-alias/130844/4 "2025-07-18T11:29:45Z")

</div>

Check the output. The final result satisfies your type restriction because `Int` is converted to `Float64`. Your variable `x` will have the type you specified, with a single `T`.

I don’t think there is a way to avoid type conversion from the RHS → LHS.

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [July 18, 2025, 2:28pm UTC](https://discourse.julialang.org/t/basic-question-about-type-alias/130844/5 "2025-07-18T14:28:50Z")

</div>

@juliohm already gave the correct answer, but I wanted to clarify two things for other readers and add links to the docs:

- as was mentioned, the `::` syntax does two different things, depending on which side of an assignment it is used; in the OP it is used on the left-hand side, which behaves essentially like a `convert` call; but if used on on an expression (so on the right-hand side) it will assert the type of the given expression and error if it doesn’t match (see also [Types · The Julia Language](https://docs.julialang.org/en/v1/manual/types/) )
- what looks additionally confusing about the second example from the op is that an assignment always returns the expression on the right-hand side (i.e. before any conversion happens), so it looks like this when run in a REPL

```julia-repl
x::VecScalar{Float64} = ([1, 2], 3.0)
([1, 2], 3.0)

```

but as was pointed out, the value of `x` will be `([1.0, 2.0], 3.0)` (see [here](https://docs.julialang.org/en/v1/manual/faq/#What-is-the-return-value-of-an-assignment?))

Just thought that extra bit of info might be useful to someon.

---

<div class="post-metadata">

**Author:** ![Nikos\_Gianniotis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nikos_gianniotis/32/11487_2.png) [@Nikos\_Gianniotis](https://discourse.julialang.org/u/Nikos_Gianniotis)\
**Post date:** [July 18, 2025, 2:40pm UTC](https://discourse.julialang.org/t/basic-question-about-type-alias/130844/6 "2025-07-18T14:40:50Z")

</div>

Spot on:

> [@Sevi](#):
>
> what looks additionally confusing about the second example from the op is that an assignment always returns the expression on the right-hand side (i.e. before any conversion happens), so it looks like this when run in a REPL

This is indeed what really confused me. Your comments really help clarify the situation.
