Referencing external code or modules in Gox language-GX15

The following is quoted from Qlang's Github page ,

include

In qlang, a .ql file can include the content of another .ql through the include grammar. The so-called inclusion, its actual ability is similar to copying and pasting code. For example, there are two files a.ql and b.ql in a certain directory.

The contents of a.ql are as follows:

println("in script A")

foo = fn() {
	println("in func foo:", a, b)
}

The contents of b.ql are as follows:

a = 1
b = 2

include "a.ql"

println("in script B")
foo()

If the file name of the include statement is not suffixed with .ql, then qlang will consider it to be a directory name and add the suffix "/main.ql" to it. In other words:

include "foo/bar.v1"

Equivalent to:

include "foo/bar.v1/main.ql"

Module and import

In qlang, a module is a directory, and a file named main.ql is required in the directory. The ident in the module is private by default. To export an ident, you need to use the export syntax. E.g:

a = 1
b = 2

println("in script A")

f = fn() {
	println("in func foo:", a, b)
}

export a, f

This module exports two idents: integer variable a and function f.

To quote this module, we need to use the import grammar:

import "foo/bar.v1"
import "foo/bar.v1" as bar2

bar.a = 100 // 将 bar.a 值设置为 100
println(bar.a, bar2.a) // bar.a, bar2.a 的值现在都是 100

bar.f()

qlang will QLANG_PATH look for foo/bar.v1/main.ql files in the directory list indicated by the  environment variable  . If the environment variable QLANG_PATHis not set  , it will be ~/qlang searched in the  directory.

Importing a module multiple times will not cause any problems. In fact, nothing will happen to the second import, just an alias is added.

include vs. import

Include is copy and paste, which is more suitable for content organization in modules. For example, if a module is more complicated and all written in the main.ql file is too verbose, you can use the include statement to split it into multiple files. Include does not QLANG_PATH find files through  , it always __dir__locates files based on  (ie the directory of the script where the include code is located).

Import is a module reference, suitable for use as the main method of business decomposition. import QLANG_PATH searches for the referenced module based on  this environment variable, not based on it  __dir__.

Guess you like

Origin blog.csdn.net/weixin_41462458/article/details/107912269