# Weird UndefVarError in Julia 1.3 using HDF5.jl

**URL:** https://discourse.julialang.org/t/weird-undefvarerror-in-julia-1-3-using-hdf5-jl/27047
**Category:** General Usage
**Tags:** question
**Created:** [July 31, 2019, 5:52pm UTC](https://discourse.julialang.org/t/weird-undefvarerror-in-julia-1-3-using-hdf5-jl/27047 "2019-07-31T17:52:42Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![shipengcheng1230](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shipengcheng1230/32/4089_2.png) [@shipengcheng1230](https://discourse.julialang.org/u/shipengcheng1230)
#### Post date: [July 31, 2019, 5:52pm UTC](https://discourse.julialang.org/t/weird-undefvarerror-in-julia-1-3-using-hdf5-jl/27047/1 "2019-07-31T17:52:42Z")

</div>

Hello, consider the following MWE:

```julia
using HDF5

u = rand(2, 3)
h5open("foo.h5", "w") do f # write some data in `foo`
    du = similar(u)
    d_create(f, "u", datatype(eltype(du)), ((2, 3, 10), (2, 3, -1,)), "chunk", (2, 3, 10))
end

A = Vector{Int}(1:10) # write some data in `bar`
h5write("bar.h5", "bar", A)
# will error
h5writeattr("bar.h5", "bar", Dict("c"=>"value for metadata parameter c", "d"=>"metadata d"))
@show h5readattr("bar.h5", "bar")

rm("bar.h5"; force=true)
rm("foo.h5"; force=true)

```

This will show error on attempting `h5writeattr`:

```julia
ERROR: UndefVarError: S not defined
Stacktrace:
 [1] datatype(::S<:String) at /home/pshi/Documents/julia-1.3.0-mkl/packages/HDF5/Y9Znv/src/HDF5.jl:1178

```

However, in the original code the `S` is well-defined:  
[https://github.com/JuliaIO/HDF5.jl/blob/9fe2e927ba1e22b27546d1392ac3e81a77719d51/src/HDF5.jl#L1177-L1182](https://github.com/JuliaIO/HDF5.jl/blob/9fe2e927ba1e22b27546d1392ac3e81a77719d51/src/HDF5.jl#L1177-L1182)

On the contrary, if you put the data creation in `foo` after calling `h5writeattr`, everything works perfectly (please restart a new Julia repl):

```julia
using HDF5

u = rand(2, 3)

A = Vector{Int}(1:10) # write some data in `bar`
h5write("bar.h5", "bar", A)
h5writeattr("bar.h5", "bar", Dict("c"=>"value for metadata parameter c", "d"=>"metadata d"))

h5open("foo.h5", "w") do f # write some data in `foo`
    du = similar(u)
    d_create(f, "u", datatype(eltype(du)), ((2, 3, 10), (2, 3, -1,)), "chunk", (2, 3, 10))
end

h5writeattr("bar.h5", "bar", Dict("e" => "more metadata"))
# will show all saved meta data
@show h5readattr("bar.h5", "bar")

rm("bar.h5"; force=true)
rm("foo.h5"; force=true)

```

Moreover, if I make either of the two changes, hard-coding data type or not using `similar` array, the problem will disappear:

```julia
d_create(f, "u", datatype(Float64), ((2, 3, 10), (2, 3, -1,)), "chunk", (2, 3, 10))

```

or

```julia
du = rand(2, 3)
d_create(f, "u", datatype(eltype(du)), ((2, 3, 10), (2, 3, -1,)), "chunk", (2, 3, 10))

```

I am confused about this emitted error which does not make sense. Also it only happens on Julia 1.3 or later, not before. I encounter this problem when wrapping `h5writeattr` in my own function.

Thanks in advance!

---

<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: [July 31, 2019, 6:03pm UTC](https://discourse.julialang.org/t/weird-undefvarerror-in-julia-1-3-using-hdf5-jl/27047/2 "2019-07-31T18:03:21Z")

</div>

Feels like a bug to me. Please open an issue.

---

<div class="post-metadata">

### Author: ![shipengcheng1230](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shipengcheng1230/32/4089_2.png) [@shipengcheng1230](https://discourse.julialang.org/u/shipengcheng1230)
#### Post date: [July 31, 2019, 6:15pm UTC](https://discourse.julialang.org/t/weird-undefvarerror-in-julia-1-3-using-hdf5-jl/27047/3 "2019-07-31T18:15:59Z")

</div>

Thanks for the reply, [here](https://github.com/JuliaLang/julia/issues/32752) is the issue.
