Keywords (or reserved words) in Julia 1.0
Keywords (or reserved words) in Julia 1.0
How do I get keyword information for the version of Julia being run?
For example, there are 33 keywords in Python 3.6, but only 31 in 2.7:
# This is Python 3.6 code running in a file:
import keyword
print(len(keyword.kwlist))
## Output: 33
# This is Python 2.7 code running in a file:
import keyword
print len(keyword.kwlist)
## Output: 31
Can this kind of check be done in Julia? Or does someone have a different suggestion for getting info on Julia keywords for the running version?
EDIT:
Thanks to responses below I have gotten some interesting suggestions. However, something seems to be missing. For example, in each list of reserved words provided below, the word elseif
is missing. Am I to believe that elseif
is not a keyword (or reserved word)?
elseif
elseif
Also when I go to the Scheme code where intial-reserved-words
and reserved-words
come from, I find the following code:
intial-reserved-words
reserved-words
(define initial-reserved-words '(begin while if for try return break continue
function macro quote let local global const do
struct
module baremodule using import export))
(define initial-reserved-word? (Set initial-reserved-words))
(define reserved-words (append initial-reserved-words '(end else catch finally true false))) ;; todo: make this more complete
At the end of the last line above is the comment:
;; todo: make this more complete
This seems to imply that even the union of intial-reserved-words
and the reserved-words
lists (or whatever they are called in Scheme) is not complete.
intial-reserved-words
reserved-words
Thus, I have held off on checking one of the answers. I will be happy to check one when I see how to get a canonical list of Julia reserved words in Julia code. If a Julia expert believes one of the suggestions below is the best way to get the list of keywords (reserved words) for the current version of Julia, I would appreciate knowing it.
I wanted to illustrate how keywords can change from version to version of a language. I am coming from a Python background and wanted to illustrate the kind of thing I am looking for in Julia with actual code in Python. Also the number of people who know Python and Julia is probably more than the number of people who know only Julia.
– Julia Learner
Sep 14 '18 at 23:20
This doesn't exactly answer your question, but you might find this hack interesting:
mapreduce(α -> Base.REPLCompletions.completions(string(α), 1)[1], vcat,String(Char(0x20):Char(0x7e)))
.– rickhg12hs
Sep 15 '18 at 12:38
mapreduce(α -> Base.REPLCompletions.completions(string(α), 1)[1], vcat,String(Char(0x20):Char(0x7e)))
Interesting hack, but the only Base.REPL... that I get at the Julia 1.0.0 REPL is Base.REPL_MODULE_REF. Maybe I am misunderstanding something?
– Julia Learner
Sep 16 '18 at 5:33
Ahhh, yeah. I'm still on v0.6.4. For v1.0.0 I think it might be
mapreduce(α -> Base.REPL_MODULE_REF.x.REPLCompletions.completions(string(α), 1)[1], vcat,String(Char(0x20):Char(0x7e)))
.– rickhg12hs
Sep 16 '18 at 9:23
mapreduce(α -> Base.REPL_MODULE_REF.x.REPLCompletions.completions(string(α), 1)[1], vcat,String(Char(0x20):Char(0x7e)))
2 Answers
2
Not actually in Julia, but maybe what you are looking for. On the command line, typing julia --lisp
will bring you to the Lisp interpreter that Julia uses for parsing, in which you can see the list of reversed words by evaluating reserved-words
(and a lot of others like operators
).
julia --lisp
reserved-words
operators
> julia --lisp
; _
; |_ _ _ |_ _ | . _ _
; | (-||||_(_)|__|_)|_)
;-------------------|----------------------------------------------------------
> reserved-words
(begin while if for try return break continue function macro quote let local
global const do struct type immutable importall module baremodule using import
export end else catch finally true false)
This is cool to know. However, I was hoping for a way to do this in simple Julia code. Maybe that is just not possible, in which case your suggestion may be the best available. I am waiting to see.
– Julia Learner
Sep 16 '18 at 4:33
Allo,
There are some keywords, you can find them on the Julia documentation.
initial-reserved-words
$ julia --lisp
; _
; |_ _ _ |_ _ | . _ _
; | (-||||_(_)|__|_)|_)
;-------------------|-------------------------------------------------------
> initial-reserved-words
(begin while if for try return break continue function macro quote let local
global const do struct module baremodule using import export)
keywords = ("begin","while","if","for","try","return","break","continue","function","macro",
"quote","let","local","global","const","do","struct","module","baremodule",
"using","import","export")
print(length(keywords)) # 22
If you are new to Julia, I recommend you take a look at:
It's a bunch of example so you can experiment on the language.
I hope it helps ;)
Thanks for the links. Yes, the Julia 1.0 documentation does identify keywords. For example, return is listed as a keyword. However, I would like to get the list of keywords programmatically, as in the Python 2.7/3.6 examples.
– Julia Learner
Sep 15 '18 at 0:19
The initial-reserved-words value appears to be in a Scheme file. I do not know Scheme and do not know how to access it from Julia. Also, the word initial troubles me. Does that mean there are more after the initial values are set?
– Julia Learner
Sep 15 '18 at 0:36
You could get them from the julia console I believe, otherwise the only way I know would be to create the array yourself.
– Sylhare
Sep 15 '18 at 0:37
How would you get them from the Julia console?
– Julia Learner
Sep 15 '18 at 0:38
try pressing tab two times it should appear, or you could use the help
-h
or with ?
command– Sylhare
Sep 15 '18 at 0:41
-h
?
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Isn't this a pure Julia question? How is Python related here?
– DYZ
Sep 14 '18 at 23:16