# Error in inner constructor of a tagged struct

**URL:** <https://discourse.julialang.org/t/error-in-inner-constructor-of-a-tagged-struct/15401>\
**Category:** General Usage\
**Tags:** question, type, parametric-types\
**Created:** [September 23, 2018, 11:12pm UTC](https://discourse.julialang.org/t/error-in-inner-constructor-of-a-tagged-struct/15401 "2018-09-23T23:12:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [September 23, 2018, 11:12pm UTC](https://discourse.julialang.org/t/error-in-inner-constructor-of-a-tagged-struct/15401/1 "2018-09-23T23:12:24Z")

</div>

When I am trying to run code like this

```julia
naturalvariables = ('T', 'P', 'V', 'S')

struct NaturalVariable{tag}
    values::Matrix
    function NaturalVariable{tag}(values) where tag
        tag ∉ naturalvariables && throw(DomainError("The tag must be in $(naturalvariables)!"))
        new(values)
    end
end

const Temperature = NaturalVariable{'T'}

```

This is the only way that goes through the type declaration and instance creation.  
However, I cannot specify the type of the tag to be a `Char`.

```julia
julia> struct NaturalVariable{tag}
           values::Matrix
           function NaturalVariable{tag::Char}(values) where tag
               tag ∉ naturalvariables && throw(DomainError("The tag must be in $(naturalvariables)!"))
               new(values)
           end
       end
ERROR: TypeError: in typeassert, expected Char, got TypeVar

```

or sometimes the declaration goes through, but I cannot create an instance:

```julia
julia> struct NaturalVariable{tag}
           values::Matrix
           function NaturalVariable{tag}(values) where tag <: Char
               tag ∉ naturalvariables && throw(DomainError("The tag must be in $(naturalvariables)!"))
               new(values)
           end
       end

julia> NaturalVariable{'T'}(rand(4,4))
ERROR: MethodError: no method matching NaturalVariable{'T'}(::Array{Float64,2})
Stacktrace:
 [1] top-level scope at none:0

```

Another case:

```julia
julia> struct NaturalVariable{tag::Char}
           values::Matrix
           function NaturalVariable{tag}(values) where tag
               tag ∉ naturalvariables && throw(DomainError("The tag must be in $(naturalvariables)!"))
               new(values)
           end
       end
ERROR: syntax: invalid variable expression in "where"

julia> const Temperature = NaturalVariable{'T'}
ERROR: TypeError: in NaturalVariable, in tag, expected tag<:Char, got Char

```

Does the parametric type only accepts types rather than instances? What’s wrong with those declarations?

---

<div class="post-metadata">

**Author:** ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Post date:** [September 24, 2018, 12:33am UTC](https://discourse.julialang.org/t/error-in-inner-constructor-of-a-tagged-struct/15401/2 "2018-09-24T00:33:33Z")

</div>

As far as I know, there is no way in the current language to specify the type of a parameter in the declaration of a parameterized type. The only way to enforce that it has a certain type is in the constructor, as you have done in your first example. For example, in `Base`, the definition of `bitarray` has the form:

```julia
mutable struct BitArray{N} <: AbstractArray{Bool, N}
...
end

```

with no qualification on `N`.
