Package Declaration
Inspect one language lane at a time so line-level text and code deltas stay readable.
Diff Lane
English
0 modified sections0 code block delta0 anchor delta
Diff Lane
中文
1 modified sections0 code block delta0 anchor delta
modified包的声明textcode+2 lines
v1.0.5
Section Text
1
在仓颉编程语言中,包声明以关键字 `package` 开头,后接 root 包至当前包由 `.` 分隔路径上所有包的包名。包名必须是合法的普通标识符(不含原始标识符)。例如:2
3
4
> **注意:**5
>6
> 当前 Windows 平台版本,包名暂不支持使用 Unicode 字符,包名必须是一个仅含 ASCII 字符的合法的普通标识符。7
8
包声明必须在源文件的非空非注释的首行,且同一个包中的不同源文件的包声明必须保持一致。9
10
<!-- compile.error -->11
<!-- cfg="-p test --output-type=staticlib" -->12
13
14
仓颉的包名需反映当前源文件相对于项目源码根目录 `src` 的路径,并将其中的路径分隔符替换为小数点。例如包的源代码位于 `src/directory_0/directory_1` 下,root 包名为 `pkg` 则其源代码中的包声明应为 `package pkg.directory_0.directory_1`。15
16
需要注意的是:17
18
- 包所在的文件夹名必须与包名一致。19
- 源码根目录默认名为 `src`。20
- 源码根目录下的包可以没有包声明,此时编译器将默认为其指定包名 `default`。21
22
假设源代码目录结构如下:23
24
25
则 `a.cj`、`b.cj`、`c.cj`、`main.cj` 中的包声明可以为:26
27
28
29
30
31
另外,包声明不能引起命名冲突:子包不能和当前包的顶层声明同名。32
33
以下是一些错误示例:34
35
36
<!-- compile.error -->37
<!-- cfg="-p a/B --output-type=staticlib" -->38
39
40
<!-- compile.error -->41
<!-- cfg="liba.a liba.B.a" -->Code 1 · cangjie
1
package pkg1 // root 包 pkg12
package pkg1.sub1 // root 包 pkg1 的子包 sub1Code 2 · cangjie
1
// file 12
// Comments are accepted3
package test4
// declarations...5
6
// file 27
let a = 1 // Error, package declaration must appear first in a file8
package test9
// declarations...Code 3 · cangjie
1
// The directory structure is as follows:2
src3
`-- directory_04
|-- directory_15
| |-- a.cj6
| `-- b.cj7
`-- c.cj8
`-- main.cjCode 4 · text
1
// a.cj2
// in file a.cj, the declared package name must correspond to relative path directory_0/directory_1.3
4
package default.directory_0.directory_1Code 5 · cangjie
1
// b.cj2
// in file b.cj, the declared package name must correspond to relative path directory_0/directory_1.3
4
package default.directory_0.directory_1Code 6 · cangjie
1
// c.cj2
// in file c.cj, the declared package name must correspond to relative path directory_0.3
4
package default.directory_0Code 7 · cangjie
1
// main.cj2
// file main.cj is in the module root directory and may omit package declaration.3
4
main(): Int64 {5
return 06
}Code 8 · cangjie
1
// a.cj2
package a3
public class B { // Error, 'B' is conflicted with sub-package 'a.B'4
public static func f() {}5
}Code 9 · cangjie
1
// b.cj2
package a.B3
public func f {}Code 10 · cangjie
1
// main.cj2
import a.B // ambiguous use of 'a.B'3
4
main() {5
a.B.f()6
}Code 11 · cangjie
1
v1.1.0
Section Text
1
在仓颉编程语言中,包声明以关键字 `package` 开头,后接 root 包至当前包由 `.` 分隔路径上所有包的包名。包名必须是合法的普通标识符(不含原始标识符)。例如:2
3
<!-- compile -->4
5
6
<!-- compile -->7
8
9
> **注意:**10
>11
> 当前 Windows 平台版本,包名暂不支持使用 Unicode 字符,包名必须是一个仅含 ASCII 字符的合法的普通标识符。12
13
包声明必须在源文件的非空非注释的首行,且同一个包中的不同源文件的包声明必须保持一致。14
15
<!-- compile.error -->16
<!-- cfg="-p test --output-type=staticlib" -->17
18
19
仓颉的包名需反映当前源文件相对于项目源码根目录 `src` 的路径,并将其中的路径分隔符替换为小数点。例如包的源代码位于 `src/directory_0/directory_1` 下,root 包名为 `pkg` 则其源代码中的包声明应为 `package pkg.directory_0.directory_1`。20
21
需要注意的是:22
23
- 包所在的文件夹名必须与包名一致。24
- 源码根目录默认名为 `src`。25
- 源码根目录下的包可以没有包声明,此时编译器将默认为其指定包名 `default`。26
27
假设源代码目录结构如下:28
29
30
则 `a.cj`、`b.cj`、`c.cj`、`main.cj` 中的包声明可以为:31
32
<!-- compile -->33
34
35
<!-- compile -->36
37
38
<!-- compile -->39
40
41
<!-- compile -->42
43
44
另外,包声明不能引起命名冲突:子包不能和当前包的顶层声明同名。45
46
以下是一些错误示例:47
48
<!-- compile -->49
<!-- cfg="-p a --output-type=staticlib" -->50
51
52
<!-- compile.error -->53
<!-- cfg="-p a/B --output-type=staticlib" -->54
55
56
<!-- compile.error -->57
<!-- cfg="liba.a liba.B.a" -->Code 1 · cangjie
1
package pkg1 // root 包 pkg1Code 2 · cangjie
1
package pkg1.sub1 // root 包 pkg1 的子包 sub1Code 3 · cangjie
1
// file 12
// Comments are accepted3
package test4
// declarations...5
6
// file 27
let a = 1 // Error, package declaration must appear first in a file8
package test9
// declarations...Code 4 · text
1
// The directory structure is as follows:2
src3
|-- directory_04
|-- directory_15
| |-- a.cj6
| |-- b.cj7
|-- c.cj8
|-- main.cjCode 5 · cangjie
1
// a.cj2
// in file a.cj, the declared package name must correspond to relative path directory_0/directory_1.3
4
package default.directory_0.directory_1Code 6 · cangjie
1
// b.cj2
// in file b.cj, the declared package name must correspond to relative path directory_0/directory_1.3
4
package default.directory_0.directory_1Code 7 · cangjie
1
// c.cj2
// in file c.cj, the declared package name must correspond to relative path directory_0.3
4
package default.directory_0Code 8 · cangjie
1
// main.cj2
// file main.cj is in the module root directory and may omit package declaration.3
4
main(): Int64 {5
return 06
}Code 9 · cangjie
1
// a.cj2
package a3
public class B { // Error, 'B' is conflicted with sub-package 'a.B'4
public static func f() {}5
}Code 10 · cangjie
1
// b.cj2
package a.B3
public func f {}Code 11 · cangjie
1
// main.cj2
import a.B // ambiguous use of 'a.B'3
4
main() {5
a.B.f()6
}