Skip to content

Commit 1c8f90d

Browse files
committed
更新泛型使用相关的文档
1 parent 6aba3a3 commit 1c8f90d

File tree

2 files changed

+28
-45
lines changed

2 files changed

+28
-45
lines changed

doc/unity/zhcn/tutorial/lang-comparison.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,16 @@ print(outB[0], refC[0]) # 100, 20
110110

111111
## 8. 泛型类型创建
112112

113-
| 语言 | 泛型类名写法 | 创建 `List<int>` |
114-
|------|-------------|-----------------|
115-
| **JavaScript** | `List$1` | `puer.$generic(CS.System.Collections.Generic.List$1, CS.System.Int32)` |
116-
| **Lua** | `List_1` | `puerts.generic(CS.System.Collections.Generic.List_1, CS.System.Int32)` |
117-
| **Python** | `List__T1`(import 时)或 `` List`1 ``(load_type 时) | `puerts.generic(List, System.Int32)` |
113+
| 语言 | 泛型类名写法 | 创建 `List<int>` |
114+
|------|------------------------------------------------|-----------------|
115+
| **JavaScript** | `List$1` | `puer.$generic(CS.System.Collections.Generic.List$1, CS.System.Int32)` |
116+
| **Lua** | `List_1` | `puerts.generic(CS.System.Collections.Generic.List_1, CS.System.Int32)` |
117+
| **Python** | `List_1``List`(import 时)或 `` List`1 ``(load_type 时) | `List[System.Int32]` |
118118

119-
> 三种语言中反引号 `` ` `` 的替代符不同:JS 用 `$`,Lua 用 `_`,Python import 时用 `__T`
119+
> Python 使用 `import XXX_1```load_type['XXX`1']`` 来获取泛型类型时,会导入类型 ``XXX`1``,而使用 `import XXX` 则会导入一个特殊的泛型工厂类型,调用时传入类型参数即可根据掺入的类型参数数量创建泛型类型实例。
120+
> 两者都可以使用 `List[System.Int32]` 来创建 `List<int>`,但前者需要(也可以用as来调整为`List`)写成 `List_1[System.Int32]`,后者则更简洁。
121+
122+
> 三种语言中反引号 `` ` `` 的替代符不同:JS 用 `$`,Lua 用 `_`,Python import 时用 `_`
120123
121124
---
122125

@@ -126,7 +129,7 @@ print(outB[0], refC[0]) # 100, 20
126129
|------|------|
127130
| **JavaScript** | 直接调用,V8 自动推断类型参数 |
128131
| **Lua** | `puerts.genericMethod(类型, '方法名', 泛型参数...)` |
129-
| **Python** | `puerts.genericMethod(类型, '方法名', 泛型参数...)` |
132+
| **Python** | `puerts.generic_method(类型, '方法名', 泛型参数...)` |
130133

131134
```lua
132135
-- Lua 示例
@@ -204,7 +207,7 @@ fn("hello");
204207

205208
| 功能 | JavaScript (`puer.xxx`) | Lua (`require('puerts').xxx`) | Python (`puerts.xxx`) |
206209
|------|------------------------|------------------------------|----------------------|
207-
| 泛型类型 | `puer.$generic()` | `puerts.generic()` | `puerts.generic()` |
210+
| 泛型类型 | `puer.$generic()` | `puerts.generic()` | 使用 import 导入泛型即可 (import List_1/List),细节参考上文对泛型的解释 |
208211
| 泛型方法 | — (自动推断) | `puerts.genericMethod()` | `puerts.genericMethod()` |
209212
| typeof | `puer.$typeof()` | `puerts.typeof()` | `puerts.typeof()` |
210213
| ref/out 创建 | `puer.$ref()` | `{}` (table) | `[]` (list) |

doc/unity/zhcn/tutorial/python2cs.md

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -173,33 +173,7 @@ print('ret=' + str(ret) + ', out=' + str(outRef[0]) + ', ref=' + str(refRef[0]))
173173
----------------------------
174174
### 泛型
175175

176-
Python 中使用 `puerts.generic()` 来创建泛型类型:
177-
178-
```csharp
179-
void Start() {
180-
var env = new Puerts.ScriptEnv(new Puerts.BackendPython());
181-
env.Eval(@"
182-
exec('''
183-
import System
184-
import System.Collections.Generic.List_1 as List
185-
186-
# create generic type: List<int>
187-
ListInt = puerts.generic(List, System.Int32)
188-
ls = ListInt()
189-
ls.Add(1)
190-
ls.Add(2)
191-
ls.Add(3)
192-
193-
print(ls.Count) # 3
194-
''')
195-
");
196-
env.Dispose();
197-
}
198-
```
199-
200-
**方式二:使用中括号语法(推荐)**
201-
202-
PuerTS 还支持 Python 风格的中括号语法来实例化泛型类型,类似于 Python 标准库中 `list[int]` 的写法:
176+
PuerTS 支持 Python 风格的中括号语法来实例化泛型类型,类似于 Python 标准库中 `list[int]` 的写法:
203177

204178
```csharp
205179
void Start() {
@@ -223,16 +197,22 @@ print(ls.Count) # 3
223197
}
224198
```
225199

226-
中括号语法 `List_1[Int32]` 等价于 `puerts.generic(List_1, Int32)`,对于多个类型参数,使用逗号分隔:`Dictionary_2[String, Int32]`
200+
以下导入语法都是等价的
201+
202+
```python
203+
import System.Collections.Generic.List_1
204+
import System.Collections.Generic.List as List
205+
from System.Collections.Generic import List
206+
List_1 = puerts.load_type('System.Collections.Generic.List`1')
207+
```
208+
209+
同时也都可以直接使用 `List[System.String]` 语法来创建泛型类型
210+
211+
对于多个类型参数,使用逗号分隔:`Dictionary_2[String, Int32]`
227212

228213
> ⚠️ **泛型类名的特殊表示**:C# 中泛型类名使用反引号表示类型参数个数(如 `` List`1 ``),而在 Python 的 `import` 语法中,反引号需要替换为 `_` 加参数个数:
229214
> - `` List`1 ```List_1`
230215
> - `` Dictionary`2 ```Dictionary_2`
231-
>
232-
> 使用 `puerts.load_type()` 时,可以直接使用反引号原始格式:
233-
> ```python
234-
> List = puerts.load_type('System.Collections.Generic.List`1')
235-
> ```
236216
237217
----------------------------
238218
### 嵌套类型
@@ -253,11 +233,11 @@ x = InnerClassA()
253233
print(x.Foo) # Hello
254234
255235
# access generic nested class
256-
InnerClassB_1 = puerts.generic(TestNestedTypes.InnerClassB_1, Int32)
236+
InnerClassB_1 = TestNestedTypes.InnerClassB_1[Int32]
257237
y = InnerClassB_1()
258238
print(y.Bar) # Hello
259239
260-
InnerClassB_2 = puerts.generic(TestNestedTypes.InnerClassB_2, Int32, String)
240+
InnerClassB_2 = TestNestedTypes.InnerClassB_2[Int32, String]
261241
z = InnerClassB_2()
262242
print(z.Bar) # Hello
263243
''')
@@ -296,14 +276,14 @@ val = arr.get_Item(0) # 等价于 C# 的 val = arr[0]
296276
print(val) # 42
297277
298278
# 同样适用于 List<T>
299-
ListInt = puerts.generic(List_1, System.Int32)
279+
ListInt = List_1[System.Int32]
300280
lst = ListInt()
301281
lst.Add(10)
302282
first = lst.get_Item(0) # 等价于 C# 的 lst[0]
303283
lst.set_Item(0, 20) # 等价于 C# 的 lst[0] = 20
304284
305285
# 同样适用于 Dictionary<TKey, TValue>
306-
DictStrInt = puerts.generic(Dictionary_2, System.String, System.Int32)
286+
DictStrInt = Dictionary_2[System.String, System.Int32]
307287
d = DictStrInt()
308288
d.set_Item('key', 100) # 等价于 C# 的 dict['key'] = 100
309289
v = d.get_Item('key') # 等价于 C# 的 v = dict['key']

0 commit comments

Comments
 (0)