# Wrapper Types

**URL:** <https://discourse.julialang.org/t/wrapper-types/14278>\
**Category:** General Usage\
**Created:** [August 29, 2018, 10:20pm UTC](https://discourse.julialang.org/t/wrapper-types/14278 "2018-08-29T22:20:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![filipebraida](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filipebraida/32/861_2.png) [@filipebraida](https://discourse.julialang.org/u/filipebraida)\
**Post date:** [August 29, 2018, 10:20pm UTC](https://discourse.julialang.org/t/wrapper-types/14278/1 "2018-08-29T22:20:32Z")

</div>

I am rebuilding my framework (recommender systems).  
I’m thinking about a new modeling of the problem and I’m testing some things.  
However, I was left with some doubts and I did not find a solution here.

I created a wrapper for the integer and it will be my identifier.  
I would like to call functions using integers and would not like to be converting it.

Is it possible to do this?

Example:

```julia
module Persa

# package code goes here
import Base: convert, promote_rule

struct ID
    value::Int
end

struct Rating{T <: Number}
    user::ID
    item::ID
    value::T
end

Base.promote_rule(::Type{ID}, ::Type{Int}) = ID
Base.convert(::Type{Persa.ID}, x::Int) = Persa.ID(x)
end # module

```

When I call this function, Julia don´t convert my type automatically.

```julia
rating = Persa.Rating(Persa.ID(1), 2, 5)

```

Thanks!

---

<div class="post-metadata">

**Author:** ![Non-Contradiction](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-contradiction/32/2208_2.png) [@Non-Contradiction](https://discourse.julialang.org/u/Non-Contradiction)\
**Post date:** [August 30, 2018, 1:05am UTC](https://discourse.julialang.org/t/wrapper-types/14278/2 "2018-08-30T01:05:57Z")

</div>

The problem here seems to be with the type parameter `T` in `struct Rating{T <: Number}`.  
Because the existence of `T`, `Persa.Rating` includes everything like `Persa.Rating{Int}`, `Persa.Rating{Float64}`, etc.  
So Julia doesn’t know which one is the target type of `Persa.Rating(x)`, and it won’t use implicit conversion at all.  
And if we give information for `T`, things will be okay, for example:

`rating = Persa.Rating{Int}(Persa.ID(1), 2, 5)`  
`rating = Persa.Rating{Float64}(Persa.ID(1), 2, 5)`

---

<div class="post-metadata">

**Author:** ![filipebraida](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filipebraida/32/861_2.png) [@filipebraida](https://discourse.julialang.org/u/filipebraida)\
**Post date:** [August 30, 2018, 1:41am UTC](https://discourse.julialang.org/t/wrapper-types/14278/3 "2018-08-30T01:41:49Z")

</div>

It worked here. Makes sense.

I’ve tried here to write an extra constructor, but it did not work either.  
The function did not remove the ambiguity.

```julia
Rating(user::ID, item::ID, value::T) where T <: Number = Rating{T}(user, item, value)

```

I think I’m going to give up this path and put directly as an integer.

Thank you!

---

<div class="post-metadata">

**Author:** ![Non-Contradiction](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-contradiction/32/2208_2.png) [@Non-Contradiction](https://discourse.julialang.org/u/Non-Contradiction)\
**Post date:** [August 30, 2018, 1:47am UTC](https://discourse.julialang.org/t/wrapper-types/14278/4 "2018-08-30T01:47:48Z")

</div>

Define  
`Rating(user, item, value::T) where T <: Number = Rating{T}(user, item, value)`  
should work.  
So julia knows to get `T` from the type of `value`.

---

<div class="post-metadata">

**Author:** ![filipebraida](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filipebraida/32/861_2.png) [@filipebraida](https://discourse.julialang.org/u/filipebraida)\
**Post date:** [August 30, 2018, 1:56am UTC](https://discourse.julialang.org/t/wrapper-types/14278/5 "2018-08-30T01:56:00Z")

</div>

So, It don’t work too.  
The error message is completely empty.  
Like:

```julia
Error: StackOverFlowError:

```

Edit:  
Works! Thanks!  
The error appears when you set it this way:  
`Rating(user, item, value::T) where T <: Number = Rating(user, item, value)`

However, this behavior is very strange.

---

<div class="post-metadata">

**Author:** ![Non-Contradiction](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-contradiction/32/2208_2.png) [@Non-Contradiction](https://discourse.julialang.org/u/Non-Contradiction)\
**Post date:** [August 30, 2018, 2:01am UTC](https://discourse.julialang.org/t/wrapper-types/14278/6 "2018-08-30T02:01:40Z")

</div>

The stack overflow error is because the method definition `Rating(user, item, value::T) where T <: Number = Rating(user, item, value)` is really a circular one, the RHS is of the form of LHS.

---

<div class="post-metadata">

**Author:** ![filipebraida](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filipebraida/32/861_2.png) [@filipebraida](https://discourse.julialang.org/u/filipebraida)\
**Post date:** [August 30, 2018, 2:06am UTC](https://discourse.julialang.org/t/wrapper-types/14278/7 "2018-08-30T02:06:21Z")

</div>

Yes, after reviewing I realized.

However, the error message did not help.
