Skip to content

How to specify the type of the read result? #275

@zhaoli-IHEP

Description

@zhaoli-IHEP

Hello. We are trying to read a JLD file directly. However, the @code_warntype gives warning that the returned result of read() is ::Any, and consequently the length of the string is also ::Any. So we guess the problem is how to specify the type of the read result. Is there anyway?

function test_JLD()

  file = jldopen( "test.jld", "r" )
  for index in 1:4
    test_str = read( file, "test$index" )
    len = length(test_str)
    if len > 125
      println( "too long" )
    end # if
  end # for index
  close(file)

  return nothing
end # function test_JLD

@code_warntype test_JLD()

And here is the message from @code_warntype

Variables
  #self#::Core.Compiler.Const(test_JLD, false)
  file::Union{JLD.JldFile, JLD.JLD00.JldFile}
  @_3::Union{Nothing, Tuple{Int64,Int64}}
  index::Int64
  test_str::Any
  len::Any

Body::Nothing
1 ─       (file = Main.jldopen("test.jld", "r"))
│   %2  = (1:4)::Core.Compiler.Const(1:4, false)
│         (@_3 = Base.iterate(%2))
│   %4  = (@_3::Core.Compiler.Const((1, 1), false) === nothing)::Core.Compiler.Const(false, false)
│   %5  = Base.not_int(%4)::Core.Compiler.Const(true, false)
└──       goto #6 if not %5
2 ┄ %7  = @_3::Tuple{Int64,Int64}::Tuple{Int64,Int64}
│         (index = Core.getfield(%7, 1))
│   %9  = Core.getfield(%7, 2)::Int64
│   %10 = file::Union{JLD.JldFile, JLD.JLD00.JldFile}
│   %11 = Base.string("test", index)::String
│         (test_str = Main.read(%10, %11))
│         (len = Main.length(test_str))
│   %14 = (len > 125)::Any
└──       goto #4 if not %14
3 ─       Main.println("too long")
4 ┄       (@_3 = Base.iterate(%2, %9))
│   %18 = (@_3 === nothing)::Bool
│   %19 = Base.not_int(%18)::Bool
└──       goto #6 if not %19
5 ─       goto #2
6 ┄       Main.close(file)
└──       return Main.nothing

As you can see the test_str::Any and len::Any are warned. Also the file variable, which is in the for loop, is warned file::Union{JLD.JldFile, JLD.JLD00.JldFile}.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions