Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nullable with subtype #94

Closed
andrewcooke opened this issue May 31, 2015 · 9 comments · Fixed by #279
Closed

Nullable with subtype #94

andrewcooke opened this issue May 31, 2015 · 9 comments · Fixed by #279

Comments

@andrewcooke
Copy link

I am having a problem when I assign a value to a Nullable which it a sub-type of the type contained by the Nullable. This works in 0.4, but not with Compat.

Here is the problem:

julia> abstract Parent

julia> immutable Child <: Parent end

julia> type Container x::Nullable{Parent} end

julia> c = Container(Child())
ERROR: `convert` has no method matching convert(::Type{Nullable{Parent}}, ::Child)
 in Container at no file

However, I have no idea how you solve this because:

julia> convert{T,S<:T}(::Type{Nullable{T}}, s::S) = Nullable{T}(s)
ERROR: T not defined
@andrewcooke
Copy link
Author

PS Otherwise, though, thanks for an amazingly useful package.

@johnmyleswhite
Copy link
Member

This pattern, convert{T,S<:T}, is what's sometimes called triangular dispatch. To my knowledge, it doesn't work in Julia yet.

I wonder if you want to be working with a parametric type for Container (e.g. Container{T}) rather than something non-concrete like Nullable{Parent}.

@andrewcooke
Copy link
Author

but the code i posted works in 0.4 (i'm using a 6 days old checkout).

   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: http://docs.julialang.org
   _ _   _| |_  __ _   |  Type "help()" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.4.0-dev+4995 (2015-05-25 20:43 UTC)
 _/ |\__'_|_|_|\__'_|  |  Commit 96cc970 (6 days old master)
|__/                   |  x86_64-suse-linux

julia> abstract Parent

julia> immutable Child <: Parent end

julia> type Container x::Nullable{Parent} end

julia> c = Container(Child())
Container(Nullable(Child()))

(i can fix my code in various ways, and it may not be great how it is, but i was raising this issue because the code above works in 0.4, but not in 0.3, even when Compat is used).

@andrewcooke
Copy link
Author

maybe my hamfisted attempt at guessing how julia works is misleading. ignore the final part of my first post if it helps.

@johnmyleswhite
Copy link
Member

I think something has changed that I don't fully understand in either (a) how type parameters work when doing dispatch or (b) the way that convert relates to constructors.

You can raise the same problem you're hitting without involving user types by executing convert(Nullable{Integer}, 1). Contrast Julia 0.3:

julia> @which convert(Nullable{Integer}, 1)
convert(T,x) at base.jl:13

with Julia 0.4:

julia> @which convert(Nullable{Integer}, 1)
convert{T}(::Type{Nullable{T}}, x::T) at nullable.jl:15

@andrewcooke
Copy link
Author

huh. well i've been poking through issues and changes and finally realised you wrote this stuff, so i am giving up :)

@johnmyleswhite
Copy link
Member

I'm sure someone who understands the issues involved better than I do will chime in. While we wait, I wonder if redesigning things to avoid having a type Nullable{Parent} will solve multiple problems.

@andrewcooke
Copy link
Author

@johnmyleswhite
Copy link
Member

That does seem relevant. And I do find the covariant matching confusing.

martinholters pushed a commit to martinholters/Compat.jl that referenced this issue Jul 13, 2016
Implement broadcasting for DataArrays/PooledDataArrays
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants