system stack: AMD64 hardware
Debian 11 linux
MATE desktop & terminal
println("What is your name?")
name2 = readln();
println( "Welcome to Julia $name2")
What is your name?
ERROR: LoadError: UndefVarError: readln not defined in Main
Suggestion: check for spelling errors or missing imports.
Stacktrace:
[1] top-level scope
@ ~/Julia26/HelloJulia2.jl:7
[2] include(mod::Module, _path::String)
@ Base ./Base.jl:306
[3] exec_options(opts::Base.JLOptions)
@ Base ./client.jl:317
[4] _start()
@ Base ./client.jl:550
in expression starting at /home/wwink9/Julia26/HelloJulia2.jl:7
Hi @dave3897, welcome to the forum. You’re looking for readline, not readln:
help?> readline
search: readline readlines readlink eachline readdir @inline real hardlink
readline(io::IO=stdin; keep::Bool=false)
readline(filename::AbstractString; keep::Bool=false)
Read a single line of text from the given I/O stream or file (defaults to
stdin). When reading from a file, the text is assumed to be encoded in
UTF-8. Lines in the input end with '\n' or "\r\n" or the end of an input
stream. When keep is false (as it is by default), these trailing newline
characters are removed from the line before it is returned. When keep is
true, they are returned as part of the line.
Return a String. See also copyline to instead write in-place to another
stream (which can be a preallocated IOBuffer).
See also readuntil for reading until more general delimiters.
Examples
≡≡≡≡≡≡≡≡
julia> write("my_file.txt", "JuliaLang is a GitHub organization.\nIt has many members.\n");
julia> readline("my_file.txt")
"JuliaLang is a GitHub organization."
julia> readline("my_file.txt", keep=true)
"JuliaLang is a GitHub organization.\n"
julia> rm("my_file.txt")
julia> print("Enter your name: ")
Enter your name:
julia> your_name = readline()
Logan
"Logan"
I never thought about it before, but from a UX perspective this really seems not to be ideal. Is there any reason why it is println and readline
instead of println and readln
or printline and readline?
Sure, we can’t rename one of the functions in Julia 1. But we could hypothetically rename one of both in Julia 2 if it ever happens. Alternatively in Julia 1: We could create an alias function.
An issue can also serve to just document a problem and can help to find the solution.
That being said both help?> readln and help?> printline list the correct function as the first suggestion and there are larger problems than this papercut.
IMO this might actually be worth an issue, API consistency is good to strive for and this is a case where we could get it by adding one extra function (and deprecate the other). (As a sidenote, println is a pretty badly named function since it doesn’t print a line, it prints some text followed by a linebreak. Were it not for other language apis, we likely would never have made a println or printline (it should probably something like print(x, newline=true) or something)
True, backwards compatibility is VERY important, but we see a lot of deprecations+alias being used in established languages like js too, so it could be worth a try.