# UndefVarError for Type Variable

**URL:** https://discourse.julialang.org/t/undefvarerror-for-type-variable/77419
**Category:** New to Julia
**Tags:** linearalgebra, error, parametric-types
**Created:** [March 4, 2022, 7:22pm UTC](https://discourse.julialang.org/t/undefvarerror-for-type-variable/77419 "2022-03-04T19:22:01Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![ps-pat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ps-pat/32/34331_2.png) [@ps-pat](https://discourse.julialang.org/u/ps-pat)
#### Post date: [March 4, 2022, 7:22pm UTC](https://discourse.julialang.org/t/undefvarerror-for-type-variable/77419/1 "2022-03-04T19:22:01Z")

</div>

I’m sure I’m missing something obvious, but here we go. I defined methods that dispatch on the type of various “special matrices” from LinearAlgebra. Here is a MWE:

```julia
using LinearAlgebra

for type ∈ [:Symmetric, :LowerTriangular]
    @eval f(::Type{$type{T}}) where T = T
end

```

The expected result is returned when called on type `LowerTriangular`:

```julia
julia> f(LowerTriangular{Float64})
Float64

```

However, when called on `Symmetric`, I get:

```julia
julia> f(Symmetric{Float64})
ERROR: UndefVarError: T not defined
Stacktrace:
 [1] f(#unused#::Type{Symmetric{Float64, S} where S<:(AbstractMatrix{<:Float64})})
   @ Main ./REPL[5]:2
 [2] top-level scope
   @ REPL[10]:1

```

Interestingly, the following implementation works:

```julia
f(::Type{Symmetric{T, S}}) where {T, S} = T

julia> f(Symmetric{Float64, Matrix{Float64}})
Float64

```

Any idea?

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [March 4, 2022, 7:56pm UTC](https://discourse.julialang.org/t/undefvarerror-for-type-variable/77419/2 "2022-03-04T19:56:01Z")

</div>

Fully specifying the type vars works for me

```julia
using LinearAlgebra

for type ∈ [:Symmetric, :LowerTriangular]
    @eval f(::Type{$type{T, A}}) where {T, A} = T
end

@show f(LowerTriangular{Float64,Matrix{Float64}})
@show f(Symmetric{Float64,Matrix{Float64}})

```

yielding

```julia
f(LowerTriangular{Float64, Matrix{Float64}}) = Float64
f(Symmetric{Float64, Matrix{Float64}}) = Float64      

```

Don’t know if this helps though.

---

<div class="post-metadata">

### Author: ![ps-pat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ps-pat/32/34331_2.png) [@ps-pat](https://discourse.julialang.org/u/ps-pat)
#### Post date: [March 4, 2022, 8:43pm UTC](https://discourse.julialang.org/t/undefvarerror-for-type-variable/77419/3 "2022-03-04T20:43:33Z")

</div>

Yeah that works for me too. I just don’t understand how I can partially specify it for one type and not for the other.

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [March 4, 2022, 9:08pm UTC](https://discourse.julialang.org/t/undefvarerror-for-type-variable/77419/4 "2022-03-04T21:08:02Z")

</div>

I understand my job here as to make things work, sometimes without understanding why. So let us ask another question. What might be the sense of

```julia
@show LowerTriangular{Float32,Matrix{Float64}}([[1, 2], [3, 4], [5, 6]])

```

? Answer:

```julia
ERROR: LoadError: TypeError: in LowerTriangular, in S, expected S<:AbstractMatrix{Float32}, got Type{Matrix{Float64}}

```

So maybe this was designed before the invention of `eltype`?

---

<div class="post-metadata">

### Author: ![ps-pat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ps-pat/32/34331_2.png) [@ps-pat](https://discourse.julialang.org/u/ps-pat)
#### Post date: [March 4, 2022, 9:19pm UTC](https://discourse.julialang.org/t/undefvarerror-for-type-variable/77419/5 "2022-03-04T21:19:22Z")

</div>

This is simply due to the definition of `LowerTriangular`: [here](https://github.com/JuliaLang/julia/blob/bf534986350a991e4a1b29126de0342ffd76205e/stdlib/LinearAlgebra/src/triangular.jl#L12-L12)

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [March 4, 2022, 9:41pm UTC](https://discourse.julialang.org/t/undefvarerror-for-type-variable/77419/6 "2022-03-04T21:41:07Z")

</div>

But at least syntactically

```julia
abstract type MyAbstractTriangular{S<:AbstractMatrix} <: AbstractMatrix{eltype(S)} end

```

is accepted.

Edit: this seems to work, too:

```julia
struct MyTriangularMatrix{S<:AbstractMatrix} <: MyAbstractTriangular{S}
    A::S
end 

function MyTriangularMatrix{T}(A::Matrix{T}) where T 
end

tr = MyTriangularMatrix([(1, 2) (3, 4) (5, 6)])
println()

```

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [March 4, 2022, 9:50pm UTC](https://discourse.julialang.org/t/undefvarerror-for-type-variable/77419/7 "2022-03-04T21:50:19Z")

</div>

> [@ps-pat](#):
>
> I just don’t understand how I can partially specify it for one type and not for the other.

This might be an implementation detail? What are you more interested in: this detail or the general idea?
