# How to add class information to a "view" of array?

**URL:** https://discourse.julialang.org/t/how-to-add-class-information-to-a-view-of-array/15045
**Category:** New to Julia
**Tags:** question
**Created:** [September 17, 2018, 3:10am UTC](https://discourse.julialang.org/t/how-to-add-class-information-to-a-view-of-array/15045 "2018-09-17T03:10:08Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![xiang0x48](https://avatars.discourse-cdn.com/v4/letter/x/53a042/32.png) [@xiang0x48](https://discourse.julialang.org/u/xiang0x48)
#### Post date: [September 17, 2018, 3:10am UTC](https://discourse.julialang.org/t/how-to-add-class-information-to-a-view-of-array/15045/1 "2018-09-17T03:10:08Z")

</div>

I’m dealing with some problems with points in both Cartesian and Polar coordinates, I need to:

1. convert between them
2. map function into Array{CartesianPoint, 1} and Array{PolarPoint, 1}.
3. load raw Array{Float32, 2} with shape (N, 3) from disk.

for 1, it would be much easier to deal with them with explicit defined struct than directly using a array with size (3).

```julia
struct CartesianPoint <: AbstractPoint
    x::Float32
    y::Float32
    z::Float32
end

struct PolarPoint <: AbstractPoint
    r::Float32
    θ::Float32
    z::Float32
end

```

Also this is necessary for multidispatch for functions with same name for these two different kind of AbstractPoint.

But for 2. it would be easier to use pure array to get lots of built-in function support, like (+),(-),norm, etc.  
Since points are actually really close to vectors. Currently I had to write some wrap code like

```julia
+(a::CartesianPoint, b::CartesianPoint) = CartesianPoint(a.x+b.x, a.y+b.y, a.z+b.z)

```

It seems a bad style.

For 3, there is really a problem, boxing and unboxing these classes is time consuming, especially when I’m dealing with a large number of them. Loading a pure array is fast, but construct them into CartesianPoints need much longer time.

I’m wondering what’s the proper pattern for this kind of problem?

Can we use pure array for store, and create only a light-weight layer “view” of them, thus parse raw data into different class, for multidispatch only?

---

<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: [September 17, 2018, 8:58am UTC](https://discourse.julialang.org/t/how-to-add-class-information-to-a-view-of-array/15045/2 "2018-09-17T08:58:51Z")

</div>

> [@xiang0x48](#):
>
> I’m wondering what’s the proper pattern for this kind of problem?

Use macros for operations that just pass through, eg see the code in [SaferIntegers.jl](https://github.com/JeffreySarnoff/SaferIntegers.jl/blob/master/src/arith_ops.jl) and many other libraries. But I guess you would need to do something more complex for the polar points.

Incidentally, it is more idiomatic to generalize with a parametric type, eg

```julia
struct CartesianPoint{T} <: AbstractPoint{T}
    x::T
    y::T
    z::T
end

```

> [@xiang0x48](#):
>
> load raw Array{Float32, 2} with shape (N, 3) from disk.

Use ` Mmap.mmap` and/or `reinterpret`.

> [@xiang0x48](#):
>
> map function into Array{CartesianPoint, 1} and Array{PolarPoint, 1}.

I am not sure what you want here.

---

<div class="post-metadata">

### Author: ![xiang0x48](https://avatars.discourse-cdn.com/v4/letter/x/53a042/32.png) [@xiang0x48](https://discourse.julialang.org/u/xiang0x48)
#### Post date: [September 17, 2018, 11:29am UTC](https://discourse.julialang.org/t/how-to-add-class-information-to-a-view-of-array/15045/3 "2018-09-17T11:29:20Z")

</div>

Thanks for your reply, thanks for your advice, macros used to be too heavy for me, since I’ve never used Lisp-like languages. I need to learn it now.

For map function into array, I’m just trying:

```julia
f.(ps)

```

where ps is an array of CartesianPoint or PolarPoint.  
The problem is that I need f act differently for array of CartesianPoint and PolarPoint, and need to use type for multidispatch, thus can not directly use a pure array.

The seems already solved by your suggested method.

---

<div class="post-metadata">

### Author: ![platawiec](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/platawiec/32/31914_2.png) [@platawiec](https://discourse.julialang.org/u/platawiec)
#### Post date: [September 17, 2018, 1:22pm UTC](https://discourse.julialang.org/t/how-to-add-class-information-to-a-view-of-array/15045/4 "2018-09-17T13:22:43Z")

</div>

You can try using [StaticArrays FieldVector](https://juliaarrays.github.io/StaticArrays.jl/stable/pages/api.html#FieldVector-1). A lot of the simple arithmetic is built in and you won’t have to repeat code.

You should define a `Base.convert` method to convert between the two types you define.
