# How to implement \`CartesianCoordinates\` using \`NamedTuple\`?

**URL:** <https://discourse.julialang.org/t/how-to-implement-cartesiancoordinates-using-namedtuple/15109>\
**Category:** General Usage\
**Tags:** type\
**Created:** [September 18, 2018, 5:43am UTC](https://discourse.julialang.org/t/how-to-implement-cartesiancoordinates-using-namedtuple/15109 "2018-09-18T05:43:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [September 18, 2018, 5:43am UTC](https://discourse.julialang.org/t/how-to-implement-cartesiancoordinates-using-namedtuple/15109/1 "2018-09-18T05:43:40Z")

</div>

Say I have the following code

```julia
axisnames = (:first, :second, :third, :fourth, :fifth, :sixth)

function CartesianCoordinates(t::Vararg{T, N}) where {N, T <: Real}
    NamedTuple{tuple(axisnames[1:N]...), NTuple{N, T}}((t))
end

```

That is, a `NamedTuple` can be constructed with up to 6 names. But I  
want to make `CartesianCoordinates` a type, not a function. Because  
I want to let the user just sees `CartesianCoordinates` when printing/showing,  
not a `NamedTuple`. At the same time, I want it to be used as a `NamedTuple`,  
so that I can directly use many predefined methods for `NamedTuple`, without writing them  
myself.  
In brief, what I want is a “super” type alias `MyType` (implemented by a `PredefinedType`), i.e., with a constructor, and shown as `MyType ` itself.  
How can I do that?

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [September 18, 2018, 12:46pm UTC](https://discourse.julialang.org/t/how-to-implement-cartesiancoordinates-using-namedtuple/15109/2 "2018-09-18T12:46:51Z")

</div>

Here’s a first attempt with julia 1.0, the interface part may be a bit hackish though

```julia
module n3bezier
 struct point3d{T}
   x::T; 
   y::T; 
   z::T; 
   end;
##...
end;
Base.length(x::Main.n3bezier.point3d{T}) where{T} = 1;
Base.iterate(x::Main.n3bezier.point3d{T}, state = 1) where{T} = state == 1 ? ([x.x, x.y, x.z], 2) : nothing;

julia> n3bezier.point3d(1,1,1)
point3d{Int64}(1, 1, 1)

```
