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

Inconsistent conversion of LazyJSON.Object to Union type #11

Open
mstetson opened this issue May 13, 2019 · 0 comments
Open

Inconsistent conversion of LazyJSON.Object to Union type #11

mstetson opened this issue May 13, 2019 · 0 comments

Comments

@mstetson
Copy link

I'm pretty new to Julia, so I'm not sure whether this is a bug in LazyJSON, a bug in Julia itself, or a bug in my understanding. I'm trying to save some values from a JSON document into a struct type that includes a Union{LazyJSON.Object, Nothing}. Depending on exactly how I call the constructor, Julia may try to call convert to convert the LazyJSON.Object to the union type. If that happens, the method that gets called is an @generated method for LazyJSON's struct conversion, which fails trying to call fieldnames() on the union type.

Here's a demonstration of the problem with LazyJSON 0.1.1 and Julia 1.1.0.

               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.1.0 (2019-01-21)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> import LazyJSON

julia> struct T s::String; u::Union{LazyJSON.Object,Nothing} end

julia> T(LazyJSON.value("\"foo\""), nothing)
T("foo", nothing)

julia> T("foo", LazyJSON.value("{\"foo\":\"bar\"}"))
T("foo", LazyJSON.Object{Nothing,String}("foo"=>"bar"))

julia> T(LazyJSON.value("\"foo\""), LazyJSON.value("{\"foo\":\"bar\"}"))
ERROR: MethodError: no method matching fieldnames(::Type{Union{Nothing, Object}})
Closest candidates are:
  fieldnames(::Core.TypeofBottom) at reflection.jl:175
  fieldnames(::Type{#s21} where #s21<:Tuple) at reflection.jl:177
  fieldnames(::DataType) at reflection.jl:172
  ...
Stacktrace:
 [1] #s71#28(::Any, ::Any, ::Any, ::Any) at /Users/micah/.julia/packages/LazyJSON/EmafJ/src/AbstractDict.jl:82
 [2] (::Core.GeneratedFunctionStub)(::Any, ::Vararg{Any,N} where N) at ./boot.jl:522
 [3] T(::LazyJSON.String{String}, ::LazyJSON.Object{Nothing,String}) at ./REPL[4]:1
 [4] top-level scope at none:0
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

No branches or pull requests

1 participant