# Type assertion for parametric constructors behaves strangely

**URL:** https://discourse.julialang.org/t/type-assertion-for-parametric-constructors-behaves-strangely/124184
**Category:** New to Julia
**Tags:** question, type, parametric-types
**Created:** [December 26, 2024, 1:30pm UTC](https://discourse.julialang.org/t/type-assertion-for-parametric-constructors-behaves-strangely/124184 "2024-12-26T13:30:59Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Wu-Chenyang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wu-chenyang/32/21694_2.png) [@Wu-Chenyang](https://discourse.julialang.org/u/Wu-Chenyang)
#### Post date: [December 26, 2024, 1:30pm UTC](https://discourse.julialang.org/t/type-assertion-for-parametric-constructors-behaves-strangely/124184/1 "2024-12-26T13:30:59Z")

</div>

```julia
struct A{T<:Real, N}
       a::T
       A{N}(a::T) where {T,N} = new{T,N}(a)
end

```

For this simple parametric type, `A{2}(1.0)` fails with a TypeError,

```julia
ERROR: TypeError: in A, in T, expected T<:Real, got a value of type Int64

```

I find this extremely unintuitive. Why is type assertion `T<:Real` in the definition of the parametric type `A` applied to a parameter of its constructor? Am I using the parametric constructors incorrectly?

---

<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: [December 26, 2024, 1:37pm UTC](https://discourse.julialang.org/t/type-assertion-for-parametric-constructors-behaves-strangely/124184/2 "2024-12-26T13:37:37Z")

</div>

In your type declaration:

> [@Wu-Chenyang](#):
>
> `struct A{T<:Real, N}`

… the type parameter `T` is restricted to subtype `Real`:

> [@Wu-Chenyang](#):
>
> `T<:Real`

`A{2}` fails with a `TypeError` because `2` doesn’t subtype `Real`. In fact, `2` is not a type at all.

> [@Wu-Chenyang](#):
>
> Why is type assertion `T<:Real` in the definition of the parametric type `A` applied to a parameter of its constructor?

“Constructors” are merely callable objects (objects with methods) that happen to be types: [Constructors are just callable objects](https://docs.julialang.org/en/v1/manual/constructors/#Constructors-are-just-callable-objects).

Note the `TypeError` gets thrown before an attempt to call the constructor could happen. Example:

```julia-repl
julia> struct A{T<:Real, N} end

julia> A{2}
ERROR: TypeError: in A, in T, expected T<:Real, got a value of type Int64

```

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [December 26, 2024, 1:47pm UTC](https://discourse.julialang.org/t/type-assertion-for-parametric-constructors-behaves-strangely/124184/3 "2024-12-26T13:47:30Z")

</div>

> [@Wu-Chenyang](#):
>
> Why is type assertion `T<:Real`

> [@nsajko](#):
>
> the type parameter `T` is restricted to subtype `Real`

In other words, it’s an upper bound on a type, not a type assertion for an instance. `typeassert(object, type)` and the syntax `object::type` in some contexts is a type assertion, and it’s not currently supported in `where` clauses, including the implicit one in `struct` definitions.

---

<div class="post-metadata">

### Author: ![Wu-Chenyang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wu-chenyang/32/21694_2.png) [@Wu-Chenyang](https://discourse.julialang.org/u/Wu-Chenyang)
#### Post date: [December 26, 2024, 2:21pm UTC](https://discourse.julialang.org/t/type-assertion-for-parametric-constructors-behaves-strangely/124184/4 "2024-12-26T14:21:26Z")

</div>

Thanks for your blazingly fast reply!

> [@nsajko](#):
>
> “Constructors” are merely callable objects (objects with methods) that happen to be types: [Constructors are just callable objects](https://docs.julialang.org/en/v1/manual/constructors/#Constructors-are-just-callable-objects).

I thought constructors are just methods that have the same name as the composite type. Being callable objects indeed makes a lot more sense. Thanks again for clearing up my confusion!

---

<div class="post-metadata">

### Author: ![frankwswang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frankwswang/32/18561_2.png) [@frankwswang](https://discourse.julialang.org/u/frankwswang)
#### Post date: [December 29, 2024, 11:15am UTC](https://discourse.julialang.org/t/type-assertion-for-parametric-constructors-behaves-strangely/124184/5 "2024-12-29T11:15:56Z")

</div>

I believe OP’s question was more about why the constructor of a parametric (composite) type can’t have a different signature of type parameters.

In other words, can a type `A{T<:Real, N1}` have a constructor `A{N2}(args...)` where `N2` corresponds to `N1` even though it’s in the same position as `T`?

If my understanding is correct, this is IMO a more [relevant issues](https://github.com/JuliaLang/julia/issues/44861) where I had the similar discussions with core developers a few years ago.
