Include and import

Include and import declarations

The include declaration imports all types, functions and constants from the specified files and their included files.
The import declaration imports only public types, functions and constants from the specified files and their included files. Public objects are defined using the pub keyword.
1
stringConst = "`" { unicode_char } "`" | stringDoubleConst
2
stringDoubleConst = `"` { unicode_char | uShort | uLong | escapedChar | byteVal } `"`
3
importDecl = "import" "{" {stringConst newline} "}"
4
includeDecl = "include" "{" {stringConst newline} "}"
Copied!
Consider the visibility of objects as a table. Let there be two files.
1
// a.g can include or import b.g
2
func afunc(i int) : return i*2
3
pub func apubfunc(i int) : return i*3
4
5
// b.g
6
func bfunc(i int) : return i*4
7
pub func bpubfunc(i int) : return i*5
Copied!
Let the c.g file can import or include a.g file. You can see what functions are visible in c.g in different situations.
include a a includes b
include a a imports b
import a a includes b
import a a imports b
afunc
visible
visible
apubfunc
visible
visible
visible
visible
bfunc
visible
bpubfunc
visible
visible

Pub declaration

The pub declaration marks the next function, type or constants as public. They can be imported with import declaration.
1
pubDecl = "pub"
2
objects = [pubDecl] (structDecl | FnDecl | ConstDecl | FunctionDecl)
Copied!
The pub keyword indicates that the next function, constants, or structure will be passed when the file is imported.
1
pub const IOTA { // public constants. Visible when include or import
2
MY1
3
MY2
4
}
5
6
const IOTA*2 { // private constants. Visible only when include
7
MY3
8
MY4
9
}
Copied!
Last modified 2yr ago