# Size-limited vector type

**URL:** <https://discourse.julialang.org/t/size-limited-vector-type/78697>\
**Category:** General Usage\
**Created:** [March 29, 2022, 8:03pm UTC](https://discourse.julialang.org/t/size-limited-vector-type/78697 "2022-03-29T20:03:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [March 29, 2022, 8:03pm UTC](https://discourse.julialang.org/t/size-limited-vector-type/78697/1 "2022-03-29T20:03:25Z")

</div>

I want to make a `Dict` whose keys are `view(MVector{5,UInt8}, 1:m)` for some `m<=5` and , such as

```julia
julia> let 
       d = Dict{MVector{5,UInt8}, Int}()
       d[@view((@MVector [1,2,3,4,5])[1:2])] = 1
       end
ERROR: DimensionMismatch("expected input array of length 5, got length 2")

```

How can I specify the type of this Dict so that its key size is bounded at compile time?

---

<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 29, 2022, 9:43pm UTC](https://discourse.julialang.org/t/size-limited-vector-type/78697/2 "2022-03-29T21:43:24Z")

</div>

Only way I can think of is to fill the `MVector` with some kind of sentinel, in this case probably `typemin(UInt8)` or `typemax(UInt8)`? But you are then probably on your way to implement `SBVector` etc. (with `B` for bounded)…

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [March 30, 2022, 4:35am UTC](https://discourse.julialang.org/t/size-limited-vector-type/78697/3 "2022-03-30T04:35:51Z")

</div>

The easiest way is to let the `Dict` constructor figure out the type for you by providing at least one key-value pair to the constructor:

```julia
julia> d = Dict(@view((@MVector [1,2,3,4,5])[1:2]) => 1)
Dict{SubArray{Int64, 1, MVector{5, Int64}, Tuple{UnitRange{Int64}}, true}, Int64} with 1 entry:
  [1, 2] => 1

julia> d[@view((@MVector [1,2,3,4,5])[1:3])] = 2
2

julia> d
Dict{SubArray{Int64, 1, MVector{5, Int64}, Tuple{UnitRange{Int64}}, true}, Int64} with 2 entries:
  [1, 2, 3] => 2
  [1, 2] => 1

```

The other option is to use `typeof`:

```julia
julia> T = typeof(@view((@MVector [1,2,3,4,5])[1:2]))
SubArray{Int64, 1, MVector{5, Int64}, Tuple{UnitRange{Int64}}, true}

julia> d = Dict{T, Int}()
Dict{SubArray{Int64, 1, MVector{5, Int64}, Tuple{UnitRange{Int64}}, true}, Int64}()

```

Generally you don’t want to try to write down complicated types like `SubArray{Int64, 1, MVector{5, Int64}, Tuple{UnitRange{Int64}}, true}` by hand, especially if they are types that come from somebody else’s package. Also, in many cases, the exact details of a type signature are not part of the public API—they’re just implementation details.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 30, 2022, 5:39am UTC](https://discourse.julialang.org/t/size-limited-vector-type/78697/4 "2022-03-30T05:39:13Z")

</div>

I just read somewhere that mutable dictionary keys are a bad idea. Why can’t you use `SVector` instead of `MVector`?
