Type Aliases
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类型别名text+1 line
v1.0.5
Section Text
1
当某个类型的名字比较复杂或者在特定场景中不够直观时,可以选择使用类型别名的方式为此类型设置一个别名。2
3
4
类型别名的定义以关键字 `type` 开头,接着是类型的别名(如上例中的 `I64`),然后是等号 `=`,最后是原类型(即被取别名的类型,如上例中的 `Int64`)。5
6
只能在源文件顶层定义类型别名,并且原类型必须在别名定义处可见。例如,下例中 `Int64` 的别名定义在 `main` 中将报错,`LongNameClassB` 类型在为其定义别名时不可见,同样报错。7
8
<!-- compile.error -->9
10
11
一个(或多个)类型别名定义中禁止出现(直接或间接的)循环引用。12
13
<!-- compile.error -->14
15
16
类型别名并不会定义一个新的类型,它仅仅是为原类型定义了另外一个名字,它有如下几种使用场景:17
18
1. 作为类型使用,例如:19
20
<!-- compile -->21
22
```cangjie23
type A = B24
class B {}25
var a: A = B() // Use typealias A as type B26
```27
28
2. 当类型别名实际指向的类型为 class、struct 时,可以作为构造器名称使用:29
30
<!-- compile -->31
32
```cangjie33
type A = B34
class B {}35
func foo() { A() } // Use type alias A as constructor of B36
```37
38
3. 当类型别名实际指向的类型为 class、interface、struct 时,可以作为访问内部静态成员变量或函数的类型名:39
40
<!-- compile -->41
42
```cangjie43
type A = B44
class B {45
static var b : Int32 = 046
static func foo() {}47
}48
func foo() {49
A.foo() // Use A to access static method in class B50
A.b51
}52
```53
54
4. 当类型别名实际指向的类型为 enum 时,可以作为 enum 声明的构造器的类型名:55
56
<!-- compile -->57
58
```cangjie59
enum TimeUnit {60
Day | Month | Year61
}62
type Time = TimeUnit63
var a = Time.Day 64
var b = Time.Month // Use type alias Time to access constructors in TimeUnit65
```66
67
需要注意的是,当前用户自定义的类型别名暂不支持在类型转换表达式中使用,参考如下示例:68
69
<!-- compile.error -->Code 1 · cangjie
1
type I64 = Int64Code 2 · cangjie
1
main() {2
type I64 = Int64 // Error, type aliases can only be defined at the top level of the source file3
}4
5
class LongNameClassA { }6
type B = LongNameClassB // Error, type 'LongNameClassB' is not definedCode 3 · cangjie
1
type A = (Int64, A) // Error, 'A' refered itself2
type B = (Int64, C) // Error, 'B' and 'C' are circularly refered3
type C = (B, Int64)Code 4 · cangjie
1
type MyInt = Int322
MyInt(0) // Error, no matching function for operator '()' function callv1.1.0
Section Text
1
当某个类型的名字比较复杂或者在特定场景中不够直观时,可以选择使用类型别名的方式为此类型设置一个别名。2
3
<!-- code_no_check -->4
5
6
类型别名的定义以关键字 `type` 开头,接着是类型的别名(如上例中的 `I64`),然后是等号 `=`,最后是原类型(即被取别名的类型,如上例中的 `Int64`)。7
8
只能在源文件顶层定义类型别名,并且原类型必须在别名定义处可见。例如,下例中 `Int64` 的别名定义在 `main` 中将报错,`LongNameClassB` 类型在为其定义别名时不可见,同样报错。9
10
<!-- compile.error -->11
12
13
一个(或多个)类型别名定义中禁止出现(直接或间接的)循环引用。14
15
<!-- compile.error -->16
17
18
类型别名并不会定义一个新的类型,它仅仅是为原类型定义了另外一个名字,它有如下几种使用场景:19
20
1. 作为类型使用,例如:21
22
<!-- compile -->23
24
```cangjie25
type A = B26
class B {}27
var a: A = B() // Use typealias A as type B28
```29
30
2. 当类型别名实际指向的类型为 class、struct 时,可以作为构造器名称使用:31
32
<!-- compile -->33
34
```cangjie35
type A = B36
class B {}37
func foo() { A() } // Use type alias A as constructor of B38
```39
40
3. 当类型别名实际指向的类型为 class、interface、struct 时,可以作为访问内部静态成员变量或函数的类型名:41
42
<!-- compile -->43
44
```cangjie45
type A = B46
class B {47
static var b : Int32 = 048
static func foo() {}49
}50
func foo() {51
A.foo() // Use A to access static method in class B52
A.b53
}54
```55
56
4. 当类型别名实际指向的类型为 enum 时,可以作为 enum 声明的构造器的类型名:57
58
<!-- compile -->59
60
```cangjie61
enum TimeUnit {62
Day | Month | Year63
}64
type Time = TimeUnit65
var a = Time.Day 66
var b = Time.Month // Use type alias Time to access constructors in TimeUnit67
```68
69
需要注意的是,当前用户自定义的类型别名暂不支持在类型转换表达式中使用,参考如下示例:70
71
<!-- compile.error -->Code 1 · cangjie
1
type I64 = Int64Code 2 · cangjie
1
main() {2
type I64 = Int64 // Error, type aliases can only be defined at the top level of the source file3
}4
5
class LongNameClassA { }6
type B = LongNameClassB // Error, type 'LongNameClassB' is not definedCode 3 · cangjie
1
type A = (Int64, A) // Error, 'A' refered itself2
type B = (Int64, C) // Error, 'B' and 'C' are circularly refered3
type C = (B, Int64)Code 4 · cangjie
1
type MyInt = Int322
MyInt(0) // Error, no matching function for operator '()' function call