# Parametric type specialization (like C++ template specialization)

**URL:** <https://discourse.julialang.org/t/parametric-type-specialization-like-c-template-specialization/14592>\
**Category:** General Usage\
**Tags:** type, parametric-types\
**Created:** [September 5, 2018, 5:54pm UTC](https://discourse.julialang.org/t/parametric-type-specialization-like-c-template-specialization/14592 "2018-09-05T17:54:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ksamtsak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksamtsak/32/10585_2.png) [@ksamtsak](https://discourse.julialang.org/u/ksamtsak)\
**Post date:** [September 5, 2018, 5:54pm UTC](https://discourse.julialang.org/t/parametric-type-specialization-like-c-template-specialization/14592/1 "2018-09-05T17:54:12Z")

</div>

Is there some way to express the following, which is like a C++ template specialization?

Let’s define a parametric type:

```julia
struct MyType{N<:Integer}
  field::GenericType
end

```

In the special case where N==1, we can do better:

```julia
struct MyType{N==1}
  field::SpecificType
end

```

---

<div class="post-metadata">

**Author:** ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)\
**Post date:** [September 5, 2018, 7:13pm UTC](https://discourse.julialang.org/t/parametric-type-specialization-like-c-template-specialization/14592/2 "2018-09-05T19:13:56Z")

</div>

You could do it via a constructor, e.g.,

```julia
struct MyType{T}
    field::T
end
function MyType(N::Integer)
    if N == 1
        MyType{SpecificType}(value)
    else
        MyType{GenericType}(value)
    end
end

```

It would be type unstable at the point of construction but that’s often not too much of an issue so long as it’s not in the middle of a speed critical loop. Using a function barrier (or simply the constant propagation optimisations) would get you back up to full speed.

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [September 5, 2018, 7:16pm UTC](https://discourse.julialang.org/t/parametric-type-specialization-like-c-template-specialization/14592/3 "2018-09-05T19:16:01Z")

</div>

No this is not possible, but there are various alternatives. I guess the most common options are

```julia
struct MyType{N,T}
    field::T
end

```

or

```julia
abstract type MyType{N} end
struct MyGeneric{N} <: MyType{N}
    field::Generic
end
struct MySpecial <: MyType{1}
    field::Special
end

```

---

<div class="post-metadata">

**Author:** ![ksamtsak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksamtsak/32/10585_2.png) [@ksamtsak](https://discourse.julialang.org/u/ksamtsak)\
**Post date:** [September 6, 2018, 10:37am UTC](https://discourse.julialang.org/t/parametric-type-specialization-like-c-template-specialization/14592/4 "2018-09-06T10:37:37Z")

</div>

Thanks for this suggestion. Indeed the result should be good, but it loses a lot in expression I believe. It is much neater if it can be sorted out at compile-time.

---

<div class="post-metadata">

**Author:** ![ksamtsak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksamtsak/32/10585_2.png) [@ksamtsak](https://discourse.julialang.org/u/ksamtsak)\
**Post date:** [September 6, 2018, 10:43am UTC](https://discourse.julialang.org/t/parametric-type-specialization-like-c-template-specialization/14592/5 "2018-09-06T10:43:05Z")

</div>

Yes, it probably is reasonable to leave the type as a parameter and wrap it with another type that chooses the specific field type.

My use case is to make a type with a specific API and build my logic around it, while retaining flexibility as to how this type implements its operations internally. For some specific instances of this parametric type I currently have an efficient implementation, whereas for others I don’t. In all cases, I want to be able to plug in a more efficient implementation in the future.

---

<div class="post-metadata">

**Author:** ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)\
**Post date:** [September 6, 2018, 11:10am UTC](https://discourse.julialang.org/t/parametric-type-specialization-like-c-template-specialization/14592/6 "2018-09-06T11:10:49Z")

</div>

FWIW if N is a constant then the constant propagation optimizations within the compiler mean that it should be sorted out at compile time.

If you absolutely want it to be done at compile time then check out [value types](https://docs.julialang.org/en/v1/manual/types/index.html#%22Value-types%22-1) but note that the constant propagation optimisations largely achieve the same effect.

If N isn’t a constant then it can’t be sorted out at compile time anyway 🙂

---

<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:** [September 6, 2018, 1:32pm UTC](https://discourse.julialang.org/t/parametric-type-specialization-like-c-template-specialization/14592/7 "2018-09-06T13:32:48Z")

</div>

Instead, you could just build your API around a collection of methods rather than a specific type. That would allow you to create as many specialized implementarions as you want, and also would allow others people to create their own implementarions in the future.

---

<div class="post-metadata">

**Author:** ![ksamtsak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksamtsak/32/10585_2.png) [@ksamtsak](https://discourse.julialang.org/u/ksamtsak)\
**Post date:** [September 6, 2018, 1:53pm UTC](https://discourse.julialang.org/t/parametric-type-specialization-like-c-template-specialization/14592/8 "2018-09-06T13:53:29Z")

</div>

Basically what @jw3126 is suggesting. I think this is what I’ll do, I’ll build my API on an abstract type and then make distinct specializations of it. Now that I think of it better, this is exactly the meaning I want to give, only expressed a bit differently. I was thinking in terms of C++ template specialization, but in the end it is very close to jw3126’s 2nd suggestion.
