# How to use colon based range syntax with other types?

**URL:** <https://discourse.julialang.org/t/how-to-use-colon-based-range-syntax-with-other-types/48346>\
**Category:** New to Julia\
**Created:** [October 14, 2020, 7:58am UTC](https://discourse.julialang.org/t/how-to-use-colon-based-range-syntax-with-other-types/48346 "2020-10-14T07:58:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![schrpe](https://avatars.discourse-cdn.com/v4/letter/s/f19dbf/32.png) [@schrpe](https://discourse.julialang.org/u/schrpe)\
**Post date:** [October 14, 2020, 7:58am UTC](https://discourse.julialang.org/t/how-to-use-colon-based-range-syntax-with-other-types/48346/1 "2020-10-14T07:58:43Z")

</div>

Hi, I’m still making my first steps with Julia.

I have the need for a Range type struct in my code. First I tried to use UnitRange{Int64}. Later I found out that for my needs some of the predefined functions on UnitRange{Int64} were not exactly how I needed them. Instead of redefining these functions, I thought about using my own struct:

```julia
struct ColumnRange
    start::Int64
    stop::Int64
end

```

This solves my problems, but I’m no longer able to use the nice colon-based syntax.

Is there anything I could do to enable this syntax:

`a = 10:12`

and make sure that the type of a is ColumnRange and not UnitRange{Int64}?

Greetings, Peter

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [October 14, 2020, 8:05am UTC](https://discourse.julialang.org/t/how-to-use-colon-based-range-syntax-with-other-types/48346/2 "2020-10-14T08:05:06Z")

</div>

No, I don’t think that’s possible without committing type piracy. You can however do something like this:

```julia
julia> struct ColumnRange
           start::Int64
           stop::Int64
       end

julia> struct MyInt <: Integer
           val::Int64
       end

julia> (::Colon)(a::MyInt, b::MyInt) = ColumnRange(a.val, b.val)

julia> MyInt(3):MyInt(12)
ColumnRange(3, 12)

```

Unfortunately doesn’t solve the problem of using prettier syntax…

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [October 14, 2020, 8:06am UTC](https://discourse.julialang.org/t/how-to-use-colon-based-range-syntax-with-other-types/48346/3 "2020-10-14T08:06:13Z")

</div>

> [@pfitzseb](#):
>
> without committing type piracy.

You can define a local `:` function:

```julia
julia> struct ColumnRange
           start::Int64
           stop::Int64
       end

julia> (:)(a, b) = ColumnRange(a, b)
: (generic function with 1 method)

julia> 1:6
ColumnRange(1, 6)

```

---

<div class="post-metadata">

**Author:** ![schrpe](https://avatars.discourse-cdn.com/v4/letter/s/f19dbf/32.png) [@schrpe](https://discourse.julialang.org/u/schrpe)\
**Post date:** [October 14, 2020, 9:29am UTC](https://discourse.julialang.org/t/how-to-use-colon-based-range-syntax-with-other-types/48346/4 "2020-10-14T09:29:29Z")

</div>

That looks nice.

However, it looks like it shadows the basic : function defined somewhere in Range.jl.

What I ultimately want is that I can pass 1:2 like arguments to functions with ColumnRange parameters.

I’ve looked into converters, but they seem not to be called for function arguments.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [October 14, 2020, 9:34am UTC](https://discourse.julialang.org/t/how-to-use-colon-based-range-syntax-with-other-types/48346/5 "2020-10-14T09:34:55Z")

</div>

> [@schrpe](#):
>
> it looks like it shadows the basic :

Yes, you can’t have the same thing mean two different things in the same scope so you have to choose what you want it to be.

> [@schrpe](#):
>
> What I ultimately want is that I can pass 1:2 like arguments to functions with ColumnRange parameters.
> 
> I’ve looked into converters, but they seem not to be called for function arguments.

Just make a function that accepts UnitRange{Int} then and do the conversion yourself? Something like:

```julia
julia> struct ColumnRange
           start::Int64
           stop::Int64
       end

julia> function f(x::ColumnRange)
           println("doing work with x::ColumnRange")
       end
f (generic function with 1 method)

julia> f(x::UnitRange{Int}) = f(ColumnRange(first(x), last(x)))
f (generic function with 2 methods)

julia> f(1:5)
doing work with x::ColumnRange

```

---

<div class="post-metadata">

**Author:** ![schrpe](https://avatars.discourse-cdn.com/v4/letter/s/f19dbf/32.png) [@schrpe](https://discourse.julialang.org/u/schrpe)\
**Post date:** [October 14, 2020, 12:26pm UTC](https://discourse.julialang.org/t/how-to-use-colon-based-range-syntax-with-other-types/48346/6 "2020-10-14T12:26:43Z")

</div>

> [@fredrikekre](#):
>
> Yes, you can’t have the same thing mean two different things in the same scope so you have to choose what you want it to be.

Now I see the logical circle in my own thinking. Your suggestion with the function that accepts UnitRange{Int} as a parameter and doing the conversion inside the function, would not solve my original problem: I wanted to redefine the transpose(UnitRange{Int64}) function. However, in a way that would not shadow the original implementation. And this seems to the logical cycle, which is not solvable: one cannot have two functions with the same name and signature and also have different implementations.

The functions that I wanted to apply a new meaning are transpose(UnitRange{Int64}) and -(UnitRange{Int64}). I will now go the pragmatic way and just use different names for these functions.

This will make my code a little bit harder to read, but the user of my code will have the nice colon based syntax.

Thanks to you all for helping me think! 😃
