How do I quit the player quit ()?

It's just a convenience, but I find it useful. Note that IPython allows clean shutdown, just like Matlab does. So in Julia it would be wise to allow aliases to overlap.

Thanks for any ideas on how to do this.

+3


source to share


2 answers


Exit to Julia

If you are using Julia from the command line, then ctrl-d works . But if your intent is to quit by typing the command, this is not possible exactly as you want, because typing quit in the REPL already has a value that returns the value associated with quit, which is the quit function.

julia> quit
quit (generic function with 1 method)

julia> typeof(quit)
Function

      

Also Python

But this is not uncommon, for example Python has similar behavior .

>>> quit
Use quit() or Ctrl-D (i.e. EOF) to exit

      



Using a macro

Using \ q can be nice in the Julia REPL, for example in the postgres REPL , but unfortunately it also makes sense already . However, if you were looking for an easy way to do this, how about a macro

julia> macro q() quit() end

julia> @q

      

Calls Julia to exit

If you put a macro definition in a .juliarc.jl file , it will be available every time the interpreter is started.

+10


source


As waTeim points out, when you type quit

in the REPL, it will just show the function itself ... and there is no way to change that behavior. You cannot execute a function without calling it, and there are a limited number of ways to call functions in the Julia syntax.

However, you can change the way the functions are displayed. This is extremely hacky and is not guaranteed to work, but if you want this behavior badly enough, here's what you can do: hack this behavior into a display method.

julia> function Base.writemime(io::IO, ::MIME"text/plain", f::Function)
           f == quit && quit()
           if isgeneric(f)
               n = length(f.env)
               m = n==1 ? "method" : "methods"
               print(io, "$(f.env.name) (generic function with $n $m)")
           else
               show(io, f)
           end
       end
Warning: Method definition writemime(IO,MIME{symbol("text/plain")},Function) in module Base at replutil.jl:5 overwritten in module Main at none:2.
writemime (generic function with 34 methods)

julia> print # other functions still display normally
print (generic function with 22 methods)

julia> quit # but when quit is displayed, it actually quits!

$

      



Unfortunately, there is no type more specific than ::Function

that, so you must completely rewrite the definition writemime(::IO,::MIME"text/plain",::Function)

by copying its implementation.

Also note that this is quite unexpected and somewhat dangerous. Perhaps some library might try to render the function quit

... causing you to lose work from this session.

+3


source







All Articles