As @gdalle pointed out, Val types do help. Also, your code can be simplified significantly:

```
function delta_tensor(total_dimension::Int64)
return DeltaTensor(total_dimension, Val(total_dimension * 2))
end
function delta_tensor(total_dimension::Int64, ::Val{tensororder}
)::Array{Float64,tensororder} where {tensororder}
shape = ntuple(Returns(2), tensororder)
return [float(allequal(i.I)) for i in CartesianIndices(shape)]
end
julia> @code_warntype delta_tensor(2, Val(4))
MethodInstance for delta_tensor(::Int64, ::Val{4})
from delta_tensor(total_dimension::Int64, ::Val{tensororder}) where tensororder in Main at /tmp/tensor.jl:20
Static Parameters
tensororder = 4
Arguments
#self#::Core.Const(DeltaTensor)
total_dimension::Int64
_::Core.Const(Val{4}())
Locals
#40::var"#40#41"
shape::NTuple{4, Int64}
Body::Array{Float64, 4}
1 β %1 = Core.apply_type(Main.Array, Main.Float64, $(Expr(:static_parameter, 1)))::Core.Const(Array{Float64, 4})
β %2 = Main.Returns(2)::Core.Const(Returns{Int64}(2))
β (shape = Main.ntuple(%2, $(Expr(:static_parameter, 1))))
β (#40 = %new(Main.:(var"#40#41")))
β %5 = #40::Core.Const(var"#40#41"())
β %6 = Main.CartesianIndices(shape::Core.Const((2, 2, 2, 2)))::Core.Const(CartesianIndices((2, 2, 2, 2)))
β %7 = Base.Generator(%5, %6)::Core.Const(Base.Generator{CartesianIndices{4, NTuple{4, Base.OneTo{Int64}}}, var"#40#41"}(var"#40#41"(), CartesianIndices((2, 2, 2, 2))))
β %8 = Base.collect(%7)::Array{Float64, 4}
β %9 = Base.convert(%1, %8)::Array{Float64, 4}
β %10 = Core.typeassert(%9, %1)::Array{Float64, 4}
βββ return %10
```

Note that I renamed the function to `delta_tensor`

to follow Julia conventions. Also, the suggested implementation is significantly more efficient:

```
(@v1.8) julia v1.8.5> @benchmark delta_tensor(2)
BenchmarkTools.Trial: 10000 samples with 962 evaluations.
Range (min β¦ max): 67.966 ns β¦ 1.820 ΞΌs β GC (min β¦ max): 0.00% β¦ 95.41%
Time (median): 78.095 ns β GC (median): 0.00%
Time (mean Β± Ο): 88.547 ns Β± 99.345 ns β GC (mean Β± Ο): 8.08% Β± 6.83%
ββββ
ββββ
β
ββββββββ β
ββββββββββββββββββββββ
β
β
ββ
β
ββββββββββββββββββ
ββββββββ
β
β
ββββ
β
68 ns Histogram: log(frequency) by time 177 ns <
Memory estimate: 256 bytes, allocs estimate: 2.
(@v1.8) julia v1.8.5> @benchmark DeltaTensor(2)
BenchmarkTools.Trial: 10000 samples with 5 evaluations.
Range (min β¦ max): 6.590 ΞΌs β¦ 1.197 ms β GC (min β¦ max): 0.00% β¦ 98.65%
Time (median): 7.534 ΞΌs β GC (median): 0.00%
Time (mean Β± Ο): 8.756 ΞΌs Β± 22.636 ΞΌs β GC (mean Β± Ο): 5.68% Β± 2.21%
β
β
ββββββ
β
βββββββ ββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββ
βββββ
ββ β
6.59 ΞΌs Histogram: log(frequency) by time 16.7 ΞΌs <
```