# Unexpected Type conversion

**URL:** <https://discourse.julialang.org/t/unexpected-type-conversion/17096>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 2, 2018, 7:06pm UTC](https://discourse.julialang.org/t/unexpected-type-conversion/17096 "2018-11-02T19:06:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PauloXavier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauloxavier/32/5897_2.png) [@PauloXavier](https://discourse.julialang.org/u/PauloXavier)\
**Post date:** [November 2, 2018, 7:06pm UTC](https://discourse.julialang.org/t/unexpected-type-conversion/17096/1 "2018-11-02T19:06:18Z")

</div>

Im trying to extend some functionality on the framework Persa and got stuck in conversion wich i dont know if its supposed to happen on creating my SparseMatrix. Example code:

```julia
abstract type AbstractRating{T <: Number}
end

##df is a dataframe wich is loaded previously and with no missings, the columns are of the right type as well, and consists of user, ratings and items, all of them are Int64

users = maximum(df[:user])
items = maximum(df[:item])
preference = Persa.Preference(df[:rating])
ratings = Persa.convert(df[:rating], preference)
# returns 14175-element Array{Persa.AbstractRating{Int64},1}

sparse(df[:user], df[:item], ratings, users, items)
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Persa.AbstractRating{Int64}
Closest candidates are:
  convert(::Type{S}, ::T<:(Union{CategoricalString{R}, CategoricalValue{T,R} where T} where R)) where {S, T<:(Union{CategoricalString{R}, CategoricalValue{T,R} where T} where R)} at /Users/pauloxavier/.julia/packages/CategoricalArrays/rQrLR/src/value.jl:86
  convert(::Type{T}, ::T) where T at essentials.jl:154
Stacktrace:
 [1] setindex!(::Array{Persa.AbstractRating{Int64},1}, ::Int64, ::Int64) at ./array.jl:769
 [2] sparse!(::Array{Int64,1}, ::Array{Int64,1}, ::Array{Persa.AbstractRating{Int64},1}, ::Int64, ::Int64, ::typeof(+),::Array{Int64,1}, ::Array{Int64,1}, ::Array{Int64,1}, ::Array{Persa.AbstractRating{Int64},1}, ::Array{Int64,1}, ::Array{Int64,1}, ::Array{Persa.AbstractRating{Int64},1}) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/SparseArrays/src/sparsematrix.jl:692
 [3] sparse(::Array{Int64,1}, ::Array{Int64,1}, ::Array{Persa.AbstractRating{Int64},1}, ::Int64, ::Int64, ::Function) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/SparseArrays/src/sparsematrix.jl:570
 [4] sparse(::Array{Int64,1}, ::Array{Int64,1}, ::Array{Persa.AbstractRating{Int64},1}, ::Int64, ::Int64) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/SparseArrays/src/sparsematrix.jl:754
 [5] top-level scope at none:0

```

When i try to build the sparse array, i get this conversion error. I want an `Array{Persa.AbstractRating{Int64},1}`.

As a matter of fact, this structure is the step by step used by Persa, wich pass the exact same parameters and datatypes.

---

<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:** [November 3, 2018, 8:32am UTC](https://discourse.julialang.org/t/unexpected-type-conversion/17096/2 "2018-11-03T08:32:43Z")

</div>

Please make an MWE (self-contained, OK if it still errors).

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![PauloXavier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauloxavier/32/5897_2.png) [@PauloXavier](https://discourse.julialang.org/u/PauloXavier)\
**Post date:** [November 5, 2018, 2:20pm UTC](https://discourse.julialang.org/t/unexpected-type-conversion/17096/3 "2018-11-05T14:20:18Z")

</div>

Sure, here it is:

Use the develop version, its not published yet.

ContextCF: [https://github.com/opauloxavier/ContextCF.jl](https://github.com/opauloxavier/ContextCF.jl)  
ContextDatasetsCF: [https://github.com/opauloxavier/ContextDatasetsCF.jl](https://github.com/opauloxavier/ContextDatasetsCF.jl)

````julia
using Revise
using Persa
using ContextCF
using ContextDatasetsCF

df = ContextDatasetsCF.InCarMusic()

context = ContextCF.DatasetContext(df)```

Error log:

ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Persa.AbstractRating{Int64}
Closest candidates are:
  convert(::Type{S}, ::T<:(Union{CategoricalString{R}, CategoricalValue{T,R} where T} where R)) where {S, T<:(Union{CategoricalString{R}, CategoricalValue{T,R} where T} where R)} at /Users/Equipe/.julia/packages/CategoricalArrays/rQrLR/src/value.jl:86
  convert(::Type{T}, ::T) where T at essentials.jl:154
Stacktrace:
 [1] setindex!(::Array{Persa.AbstractRating{Int64},1}, ::Int64, ::Int64) at ./array.jl:769
 [2] sparse!(::Array{Int64,1}, ::Array{Int64,1}, ::Array{Persa.AbstractRating{Int64},1}, ::Int64, ::Int64, ::typeof(+), ::Array{Int64,1}, ::Array{Int64,1}, ::Array{Int64,1}, ::Array{Persa.AbstractRating{Int64},1}, ::Array{Int64,1}, ::Array{Int64,1}, ::Array{Persa.AbstractRating{Int64},1}) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/SparseArrays/src/sparsematrix.jl:692
 [3] sparse(::Array{Int64,1}, ::Array{Int64,1}, ::Array{Persa.AbstractRating{Int64},1}, ::Int64, ::Int64, ::Function) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/SparseArrays/src/sparsematrix.jl:570
 [4] sparse(::Array{Int64,1}, ::Array{Int64,1}, ::Array{Persa.AbstractRating{Int64},1}, ::Int64, ::Int64) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/SparseArrays/src/sparsematrix.jl:754
 [5] Persa.Dataset(::DataFrames.DataFrame, ::Int64, ::Int64) at /Users/Equipe/.julia/dev/Persa/src/dataset.jl:55
 [6] Persa.Dataset(::DataFrames.DataFrame) at /Users/Equipe/.julia/dev/Persa/src/dataset.jl:77
 [7] ContextCF.DatasetContext(::DataFrames.DataFrame, ::Dict{Symbol,Type}) at /Users/Equipe/.julia/dev/ContextCF/src/dataset.jl:55
 [8] ContextCF.DatasetContext(::DataFrames.DataFrame) at /Users/Equipe/.julia/dev/ContextCF/src/dataset.jl:34
 [9] top-level scope at none:0

````

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 5, 2018, 7:30pm UTC](https://discourse.julialang.org/t/unexpected-type-conversion/17096/4 "2018-11-05T19:30:04Z")

</div>

FWIW, the reality is that you unlikely to get much help when it requires adding a bunch of packages etc.

If you could do the work to extract the non-working code into a minimal example that people can just copy paste in the REPL the chance of getting help is probably 10x larger.

---

<div class="post-metadata">

**Author:** ![PauloXavier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauloxavier/32/5897_2.png) [@PauloXavier](https://discourse.julialang.org/u/PauloXavier)\
**Post date:** [November 6, 2018, 12:16am UTC](https://discourse.julialang.org/t/unexpected-type-conversion/17096/5 "2018-11-06T00:16:00Z")

</div>

Yes, i’ll try to extract the code, i cant reproduce it very well atm. The issue is described here: [https://github.com/JuliaLang/julia/issues/27015](https://github.com/JuliaLang/julia/issues/27015) , and here [https://github.com/JuliaLang/julia/issues/27494](https://github.com/JuliaLang/julia/issues/27494) .
