Return type of readline function

Suppose you want to read from stdin:

function main()
    println("Reading...")
    readline()
end

For this code, @code_warntype says the return type is Any, even though it seems really unlikely to me that readline returns non-String value.

Can it really occur?

It says String for me (Julia 1.10.3):

julia> @code_warntype main()
MethodInstance for main()
  from main() @ Main REPL[9]:1
Arguments
  #self#::Core.Const(main)
Body::String
1 ─      Main.println("Reading...")
│   %2 = Main.readline()::String
└──      return %2
3 Likes

Interesting…

@stevengj

Could you tell me about your environment?
I’m using Julia 1.10.4 on archlinux.

Here is the implementation of readline:

function readline(s::IOStream; keep::Bool=false)
    @_lock_ios s ccall(:jl_readuntil, Ref{String}, (Ptr{Cvoid}, UInt8, UInt8, UInt8), s.ios, '\n', 1, keep ? 0 : 2)
end

I just updated to Julia 1.10.4, same result. I’m on macOS (arm64-apple-darwin22.4.0).

1 Like