# StaticRanges.jl - Static and mutable range types

**URL:** <https://discourse.julialang.org/t/staticranges-jl-static-and-mutable-range-types/30616>\
**Category:** Package Announcements\
**Tags:** package\
**Created:** [November 2, 2019, 12:19pm UTC](https://discourse.julialang.org/t/staticranges-jl-static-and-mutable-range-types/30616 "2019-11-02T12:19:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [November 2, 2019, 12:19pm UTC](https://discourse.julialang.org/t/staticranges-jl-static-and-mutable-range-types/30616/1 "2019-11-02T12:19:31Z")

</div>

Just released the second minor version of my first package (was still trying to figure out all the registering stuff in v0.1.0).

> **[GitHub - Tokazama/StaticRanges.jl: StaticArray style ranges](https://github.com/Tokazama/StaticRanges.jl)**
>
> StaticArray style ranges

It is meant to mirror the behavior of base as closely as possible. So if you know how to use `range` in base you can get mutable ranges with `mrange` and static ranges with `srange` using the same syntax. The one difference is that you can call `mrange(1, 10)` without any extra keyword arguments. This is primarily because I can’t overload the colon and got tired of typing `mrange(1, stop=10)`.

## Features

- More comprehensive coverage of ranges than StaticArrays. There are parallel types for:
  - OneTo
  - UnitRange
  - StepRange
  - LinRange
  - StepRangeLen

- Easier syntax for static ranges than is available in StaticArrays. You can just use `srange` instead of `SUnitRange`.
- Includes mutable counterparts as well as static.

## Remaining Work

- Testing: At this point the majority of future work will focus on testing and documenting examples. I have most of the tests from [base Julia](https://github.com/JuliaLang/julia/blob/master/test/ranges.jl) reimplemented for this package. Also note that test coverage should be at least at 70% but an entire file that covers `promote_rule` is inlined away. However, a lot of operators (`*`, `+`, `-`, etc) need to be more robustly tested.
- Performance: Although the user facing API should be fairly stable at this point there will likely be some internal changes as I continue to improve performance. I will update this post with performance measures as soon as they are in a more consumable form.
- Documentation: Every new method and type should have a docstring but most don’t have examples yet. I’ll probably focus on this after I get more test coverage as most behavior should be pretty intuitive if you have used ranges in Julia before.

Feel free to file issues as you find them and of course pull requests solving issues are welcome.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 2, 2019, 12:44pm UTC](https://discourse.julialang.org/t/staticranges-jl-static-and-mutable-range-types/30616/2 "2019-11-02T12:44:00Z")

</div>

> [@Zach\_Christensen](#):
>
> The one difference is that you can call `mrange(1, 10)` without any extra keyword arguments.

Note that you can also do `range(1, 10)`, I think since 1.1.

Can you please demonstrate a use case for this package?

Also, are you aware of `StaticArrays.SUnitRange`?

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [November 2, 2019, 1:26pm UTC](https://discourse.julialang.org/t/staticranges-jl-static-and-mutable-range-types/30616/3 "2019-11-02T13:26:52Z")

</div>

> [@Tamas\_Papp](#):
>
> Note that you can also do `range(1, 10)` , I think since 1.1

On 1.3 you need a keyword argument also (at least for my version).

> [@Tamas\_Papp](#):
>
> Can you please demonstrate a use case for this package?

I’ve updated the original post but will have better examples as I plot out the benchmarks. I created this package primarily because I need it for other things so I will also link to those as soon as they are ready.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 2, 2019, 1:43pm UTC](https://discourse.julialang.org/t/staticranges-jl-static-and-mutable-range-types/30616/4 "2019-11-02T13:43:25Z")

</div>

> [@Zach\_Christensen](#):
>
> On 1.3 you need a keyword argument also (at least for my version).

Sorry, I was not clear: of course you need either `step` or `length`, but `stop` does not have to be a keyword since 1.1.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 2, 2019, 2:01pm UTC](https://discourse.julialang.org/t/staticranges-jl-static-and-mutable-range-types/30616/5 "2019-11-02T14:01:29Z")

</div>

Sorry to be obtuse, but what’s static about eg [`UnitMRange`](https://github.com/Tokazama/StaticRanges.jl/blob/bafa1543cf4c62474a2593fee272c309972c1546/src/staticunitrange.jl#L52)?

Your code has

```julia
mutable struct UnitMRange{T<:Real} <: StaticUnitRange{T}
    start::T
    stop::T

    function UnitMRange{T}(start, stop) where {T<:Real}
        return new(start, Base.unitrange_last(start,stop))
    end
end

```

and the length does not seem to be encoded in the type. It just looks like a mutable `Base.UnitRange`, which I am not entirely sure is a good idea.

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [November 2, 2019, 2:11pm UTC](https://discourse.julialang.org/t/staticranges-jl-static-and-mutable-range-types/30616/6 "2019-11-02T14:11:03Z")

</div>

Yeah, that’s a terrible name for the supertype, but the most appropriate name was `AbstractUnitRange`. I just stuck with the convention from StaticArrays where everything is a subtype of `StaticArray` even if there are non static dimensions.

> [@Tamas\_Papp](#):
>
> which I am not entirely sure is a good idea.

It probably isn’t very helpful most of the time, but I have some very specific use cases where I wanted to use it. For example, I have some non mutable structs that also have a range that needs to change sizes, but everything else is the same.
