# Long Intro to Julia: type tutorial exercise

**URL:** <https://discourse.julialang.org/t/long-intro-to-julia-type-tutorial-exercise/19930>\
**Category:** New to Julia\
**Created:** [January 22, 2019, 2:03pm UTC](https://discourse.julialang.org/t/long-intro-to-julia-type-tutorial-exercise/19930 "2019-01-22T14:03:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul-Yuchao-Dong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul-yuchao-dong/32/15553_2.png) [@Paul-Yuchao-Dong](https://discourse.julialang.org/u/Paul-Yuchao-Dong)\
**Post date:** [January 22, 2019, 2:03pm UTC](https://discourse.julialang.org/t/long-intro-to-julia-type-tutorial-exercise/19930/1 "2019-01-22T14:03:09Z")

</div>

To be honest, I didn’t quite get this tutorial. [I am referring to this one about type, which I think is critical](https://github.com/JuliaComputing/JuliaBoxTutorials/blob/master/introductory-tutorials/intro-to-julia/long-version/120%20Types.ipynb)

I got to this exercise towards the end

> Define a `UnitPoint{<:Real}` parametric struct type which has `x` and `y` fields of type `T` and which has an inner constructor that normalizes its arguments by diving them by `hypot(x, y)` upon construction, guaranteeing that the resulting point is on the unit circle.

my attempt below (it’s called UnitPoint3 because the previous attempts seems to prevent me from redefining a struct, so anyone who knows how to redefine a struct can lend a hand thanks).

```julia
struct UnitPoint3{T<:Real}
    x :: T
    y :: T
    function UnitPoint3(x::T,y::T) 
        distance = hypot(x,y)
        return new(x / distance, y / distance)
    end

end

```

Now this code gave an error `syntax: too few type parameters specified in "new{...}"`

How should I fix this? Is there a more step by step intro to the type system?

---

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [January 22, 2019, 2:21pm UTC](https://discourse.julialang.org/t/long-intro-to-julia-type-tutorial-exercise/19930/2 "2019-01-22T14:21:59Z")

</div>

You need `new{T}`, this T is the type parameter it is complaining about. You will also need a `where T` at the top of function defn, I think.

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [January 22, 2019, 4:12pm UTC](https://discourse.julialang.org/t/long-intro-to-julia-type-tutorial-exercise/19930/3 "2019-01-22T16:12:46Z")

</div>

> [@Paul-Yuchao-Dong](#):
>
> How should I fix this?

The following should work:

```julia
struct UnitPoint3{T<:Real}
    x :: T
    y :: T
    function UnitPoint3{T}(x,y) where {T<:Real}
        distance = hypot(x,y)
        return new(x / distance, y / distance)
    end
end

```

```julia
julia> UnitPoint3{Float64}(1,1)
UnitPoint3{Float64}(0.7071067811865475, 0.7071067811865475)

```

The documentation contains more details about [Parametric Constructors](https://docs.julialang.org/en/v1/manual/constructors/#Parametric-Constructors-1).

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [January 22, 2019, 4:49pm UTC](https://discourse.julialang.org/t/long-intro-to-julia-type-tutorial-exercise/19930/4 "2019-01-22T16:49:43Z")

</div>

That version of the introductory materials is a brand new iteration of the content, and we’re still working on improving them. Indeed some of the exercises are far too difficult without more description. Sorry you got tripped up by it! @ffevotte’s solution is a good one.

Just to answer your other question: it’s unfortunately not possible to redefine a struct. You can re-evaluate it as long as you don’t change it significantly, but of course as you’re trying different things of course you want to significantly change the definition. The workaround you found (just iteratively changing names) is one I’ve used in the past — once I get a form I like I just rename everything back to the name I originally wanted and restart. Another workaround is to use modules since they can be replaced, but that can get tricky fast as you can end up with objects that are still the old type but have (what looks to be) the same name.

---

<div class="post-metadata">

**Author:** ![Paul-Yuchao-Dong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul-yuchao-dong/32/15553_2.png) [@Paul-Yuchao-Dong](https://discourse.julialang.org/u/Paul-Yuchao-Dong)\
**Post date:** [January 23, 2019, 3:06am UTC](https://discourse.julialang.org/t/long-intro-to-julia-type-tutorial-exercise/19930/5 "2019-01-23T03:06:24Z")

</div>

@ffevotte many thanks for your tip!

I tried your solution and indeed it worked for `UnitPoint3{Float64}(1,1)`, but it errored when I tried `UnitPoint3{Int64}(1,1)` with a message `InexactError: Int64(Int64, 0.7071067811865475)`

if this is the intended behavior, then wouldn’t this parameterization limit the struct rather than make it more general?

thanks!

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [January 23, 2019, 6:33am UTC](https://discourse.julialang.org/t/long-intro-to-julia-type-tutorial-exercise/19930/6 "2019-01-23T06:33:44Z")

</div>

try this

```julia
function unitized(x::T, y::T) where {T<:Real}
  h = hypot(x, y)
  return x/h, y/h
end
    
struct UnitPoint{T<:Real}
  x::T
  y::T

  function UnitPoint(x::T, y::T) where {T<:Real}
     ux, uy = unitized(x, y)
     return new{float(T)}(ux, uy)
  end
end

```

```julia
julia> UnitPoint(1,1)
UnitPoint{Float64}(0.7071067811865475, 0.7071067811865475)

julia> UnitPoint(1.0,1.0)
UnitPoint{Float64}(0.7071067811865475, 0.7071067811865475)

julia> UnitPoint(1//1, 1//1)
UnitPoint{Float64}(0.7071067811865475, 0.7071067811865475)

julia> UnitPoint(1.0f0, 1.0f0)
UnitPoint{Float32}(0.70710677f0, 0.70710677f0)

```

`hypot(x,y)` always returns a floating point type (it takes a sqrt).
