Is there something like Python `if __name__ ==" __main __ ":` stuff in Common Lisp

I wrote two functions in two separate files, assumed to be A.lisp and B.lisp, where both files have some basic code for the test, and A.lisp will call the function in B.lisp. This means that using the load method directly in A.lisp, the main code in B.lisp will be executed, which is not intended. And these files, in my opinion, are too small to be considered as something like a package.

Is there something like Python if __name__ == "__main__": in Common Lisp? The top-level code that is completed within this condition will be executed only if the current file is the main module, that is, a running program, but not imported as a library.

+4
source share
4 answers

Packages are just namespaces for characters. They say nothing about downloading or compiling code.

General Lisp has no idea about a library, module, or even something like the โ€œmainโ€ module / subroutine in the ANSI Common Lisp standard. The standard defines two routines PROVIDE and REQUIRE . But they are not very well indicated.

Most applications and libraries use the "system" tool to structure, specify, compile, and download code.

There is a "free" one called ASDF . "Another system definition mechanism." For most types of applications, a tool such as ASDF is useful. For primitive applications, you can create your own tools using standard functions such as COMPILE-FILE and LOAD .

Nick Levine wrote a tutorial for ASDF (part of his abandoned Lisp book project): Systems .

Several Lisp implementations have more features for building applications (for example, Mac OS X applications written with Clozure Common Lisp).

+6
source

The idiom if __name__ == "__main__": very specific to Python, and even some people in the Python community are considering using it for bad style test code.

If Common Lisp focuses on interactive development in REPL, it would actually be a nuisance to reload the entire file every time you want to run tests. Just put the tests in the function definition instead of the top level, it works and is more convenient.

+4
source

Top-level forms are evaluated.

You can define the main function, as in the following example, and you can call this function, anything:

a.lisp

 (defun main () ...) 

b.lisp

 (load "a.lisp") (main) 
+2
source

I really don't know how this works in Python, but it looks like the file module and two files are two different modules. This is not so. General Lisp. If you do not define any package when you load file, all definitions will be available in the current package ( CL-USER is by default). It is more like you entered your code in the REPL.

But packages are not very difficult to use.

in A.lisp:

 (defpackage foo (:use #:cl) (:export main)) (in-package :foo) (defun main () (do-some-stuff) (baz:main some-args)) 

in B.lisp:

 (defpackage baz (:use #:cl) (:export main)) (in-package :baz) (defun main (some-args) (do-some-stuff)) 

You can read the Complete Guide for Idiots on Common Lisp Packages and the PCL chapter for packages .

+1
source

Source: https://habr.com/ru/post/1389084/


All Articles