Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Equivalent of __name__ from Python? #277

Closed
thiagokokada opened this issue Oct 5, 2019 · 1 comment
Closed

Equivalent of __name__ from Python? #277

thiagokokada opened this issue Oct 5, 2019 · 1 comment

Comments

@thiagokokada
Copy link

Multiple script languages have an idiom to detect if the current file is being imported by another file or is the main file of the program (in this case, being the main file means that this is the file that started the program).

For example, in Python we have this idiom (an explanation what is happening here can be found in https://stackoverflow.com/a/419185):

if __name__ == "__main__":
    main()

This is useful since it allows the file to be imported by other files, however at the same time it allows the file to be executed directly. This doesn't make a lot of sense in compiled languages like Clojure (since they only have one entrypoint), however in script languages they make more sense since you can basically execute any as a full program.

Having something like a variable called *name* for example would allow for something like:

(when (= *name* "__main__")
  (main))

Btw, this is common enough that Hy for example has a (defmain) to be used in those cases: https://docs.hylang.org/en/stable/language/api.html#defmain. However this may be too much.

@candid82
Copy link
Owner

candid82 commented Oct 6, 2019

This is addressed by 920313c.
You can now do

(when (= *main-file* *file*)
  (main))

@candid82 candid82 closed this as completed Oct 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants