Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keywords (or reserved words) in Julia 1.0

Tags:

julia

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)?

Also when I go to the Scheme code where intial-reserved-words and reserved-words come from, I find the following code:

(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.

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.

like image 724
Julia Learner Avatar asked Dec 07 '22 13:12

Julia Learner


2 Answers

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
(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)
like image 132
张实唯 Avatar answered Dec 22 '22 08:12

张实唯


Allo,

There are some keywords, you can find them on the Julia documentation.

  • If you look at the Julia repository on github, there's a value initial-reserved-words containing all keywords. You can access it from the command line:
$ 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)
  • Or you can create an array yourself:
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:

  • Learn Julia in Y minutes

It's a bunch of example so you can experiment on the language. I hope it helps ;)

like image 31
Sylhare Avatar answered Dec 22 '22 06:12

Sylhare