# Using type system for different coordinate systems?

**URL:** <https://discourse.julialang.org/t/using-type-system-for-different-coordinate-systems/39722>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 18, 2020, 10:27pm UTC](https://discourse.julialang.org/t/using-type-system-for-different-coordinate-systems/39722 "2020-05-18T22:27:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![benmatwil](https://avatars.discourse-cdn.com/v4/letter/b/c2a13f/32.png) [@benmatwil](https://discourse.julialang.org/u/benmatwil)\
**Post date:** [May 18, 2020, 10:27pm UTC](https://discourse.julialang.org/t/using-type-system-for-different-coordinate-systems/39722/1 "2020-05-18T22:27:12Z")

</div>

I’m reasonably new to using Julia but I’ve been trying to rewrite a Fortran package in Julia. It mainly runs in a Cartesian-like coordinate system with very small sections that require some specialisation for different coordinate systems.  
I was hoping to be write these small sections with multiply dispatched functions for each different coordinate system using the types of the vector field `struct` I’ve been using so far. Although looking through the documentation on types, I wasn’t entirely sure how to achieve it.

Right now, I have a `struct` containing containing various arrays which include the Vector Field and coordinates. One option seemed to be to have a different struct for each coordinate system:

```julia
struct CartesianField
    ...
end

struct SphericalField
   ...
end

```

and then have each of these to be a subtype of some `AbstractField` type.

Given I’m happy for each struct to have the same internal field names and could become very verbose for different coordinate systems, I wondered if I could use some sort of parametric type instead:

```julia
struct Field{T}
   ...
end

```

where `T` is the different coordinate system types. I noticed in one place in the documentation it seemed it might be possible to use Julia symbols for T (which I wasn’t sure why that worked) or perhaps different `struct`s need to be created for each coordinate system which subtype some `AbstractCoordinateSystem`. What to use etc. is where I’m not sure. What might be best to go about this?

---

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [May 19, 2020, 1:19am UTC](https://discourse.julialang.org/t/using-type-system-for-different-coordinate-systems/39722/2 "2020-05-19T01:19:33Z")

</div>

You _can_ definitely have something like `Field{:Cartesian}`, `Field{:Polar}`, etc. I don’t think that’s the best way to go about this, though. The `Field{T}` approach means you cannot ever specialise the type based on the coordinate system. Also, the REPL and any decent editor allows you to type something like `CartesianField` using autocomplete, while `Field{:Cartesian}` would most likely not be auto-suggested. This might be a slightly silly argument, but it’s one thing which frequently annoyed me with `Field{T}`-type constructs.

---

<div class="post-metadata">

**Author:** ![benmatwil](https://avatars.discourse-cdn.com/v4/letter/b/c2a13f/32.png) [@benmatwil](https://discourse.julialang.org/u/benmatwil)\
**Post date:** [May 20, 2020, 9:58pm UTC](https://discourse.julialang.org/t/using-type-system-for-different-coordinate-systems/39722/3 "2020-05-20T21:58:54Z")

</div>

Thanks for you help, can you actually use any variable to parameterise a type? I think initially I thought that you could only use other types

---

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [May 21, 2020, 1:13am UTC](https://discourse.julialang.org/t/using-type-system-for-different-coordinate-systems/39722/4 "2020-05-21T01:13:02Z")

</div>

Surprisingly, the [documentation](https://docs.julialang.org/en/v1/manual/types/#%22Value-types%22-1) isn’t quite clear on this. I believe, the answer is that you can parametrise using types or any `isbits` value. Here’s an example:

```julia
julia> struct Foo{T}
           x::T
       end

julia> isbitstype(Foo{Int})
true

julia> Val(Foo(1)) 
Val{Foo{Int64}(1)}()

julia> isbitstype(Foo{String})
false

julia> Val(Foo("one"))
ERROR: TypeError: in Type, in parameter, expected Type, got Foo{String}
Stacktrace:
 [1] Val(::Foo{String}) at ./essentials.jl:698
 [2] top-level scope at REPL[11]:1

```

Being able to parametrise on values is useful e.g. for Julia’s `Array{T,N}` type. Here, `T` stands for the element type (e.g. `Int` or `Float64`), and `N` denotes the dimension of the array (e.g. `Vector{T} == Array{T,1}`, `Matrix{T} == Array{T,2}`).

---

<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:** [May 21, 2020, 6:33am UTC](https://discourse.julialang.org/t/using-type-system-for-different-coordinate-systems/39722/5 "2020-05-21T06:33:30Z")

</div>

> [@ettersi](#):
>
> Surprisingly, the [documentation](https://docs.julialang.org/en/v1/manual/types/#%22Value-types%22-1) isn’t quite clear on this.

cf [Types · The Julia Language](https://docs.julialang.org/en/v1/manual/types/#man-types-1:)

> Both abstract and concrete types can be parameterized by other types. They can also be parameterized by symbols, by values of any type for which [`isbits`](https://docs.julialang.org/en/v1/base/base/#Base.isbits) returns true (essentially, things like numbers and bools that are stored like C types or `struct` s with no pointers to other objects), and also by tuples thereof. Type parameters may be omitted when they do not need to be referenced or restricted.

---

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [May 21, 2020, 6:42am UTC](https://discourse.julialang.org/t/using-type-system-for-different-coordinate-systems/39722/6 "2020-05-21T06:42:43Z")

</div>

Missed this, thanks for pointing it out.

Maybe it is worth mentioning this also in other parts of the documentation. I checked the “Parametric Types” and “Value Types” section and couldn’t find the precise list there.

If people agree, I can file a PR.

---

<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:** [May 21, 2020, 7:17am UTC](https://discourse.julialang.org/t/using-type-system-for-different-coordinate-systems/39722/7 "2020-05-21T07:17:40Z")

</div>

Thanks for doing this! A PR would be great. I think that the list of things that can serve as type parameters

1. should be moved to the [Parametric Types](https://docs.julialang.org/en/v1/manual/types/#Parametric-Types-1) section,

2. written as an enumerated list for better visibility.

---

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [May 21, 2020, 8:49am UTC](https://discourse.julialang.org/t/using-type-system-for-different-coordinate-systems/39722/8 "2020-05-21T08:49:57Z")

</div>

> <https://github.com/JuliaLang/julia/pull/35971>
>
> This should make the list of possible type parameters easier to find and easier …to parse. 
> 
> See also https://discourse.julialang.org/t/using-type-system-for-different-coordinate-systems/39722.

I am not very used to creating PRs, so please let me know if I messed something up.
