playbook/codex/skills/tsl-guide/references/advanced.md

231 lines
3.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# TSL 高级特性
> 本文档是 `$tsl-guide` 的子文档,覆盖模块/对象/扩展语法与新一代特性。
> 详细规则以 `docs/tsl/syntax_book/` 为准。
## 目录
- [Unit/命名空间/函数文件](#unit命名空间函数文件)
- [Class/Object 模型](#classobject-模型)
- [扩展语法:矩阵/集合/过滤](#扩展语法矩阵集合过滤)
- [TS-SQLselect/insert/update/delete](#ts-sqlselectinsertupdatedelete)
- [运行时与性能语法要点](#运行时与性能语法要点)
- [新一代语法概览](#新一代语法概览)
---
## Unit/命名空间/函数文件
### Unit 基本结构
```tsl
Unit Demo_Unit;
interface
var Ua,Ub;
const CS = 888;
function AddV();
implementation
var Uc;
function AddV();
begin
Ua += 10;
Uc += 1;
end;
initialization
Ua := 100;
Ub := "Tinysoft Unit";
Uc := 1;
finalization
echo "Demo_Unit End.";
end.
```
### uses 与调用优先级
```tsl
uses Demo_Unit;
// 直接调用(按 uses 优先级解析)
AddV();
// 指定单元调用
Unit(Demo_Unit).AddV();
CALL("Demo_Unit.AddV");
```
### 函数文件TSF与命名空间
- `.tsf` 文件可存放 function/class/unit函数库目录 `funcext`
- 命名空间:
```tsl
NameSpace "ProjectA";
SomeFunction();
```
如需固定命名空间,可在 `tsl.Conf` 配置 `Namespace=...`(详见 `04_modules_and_namespace.md`)。
---
## Class/Object 模型
### 类定义与继承
```tsl
Type Base = Class
function Speak(); virtual;
end;
Type Child = Class(Base)
function Speak(); override;
end;
```
### 构造函数与析构函数
```tsl
Type User = Class
user_id;
user_name;
function Create(id,name); overload;
Begin
user_id := id;
user_name := name;
End;
function Destroy();
Begin
// 清理资源
End;
end;
```
### 属性Property
```tsl
Type Account = Class
_id;
function getId();
function setId(v);
property Id read _id write setId;
end;
```
### 创建对象
```tsl
Obj := CreateObject("User", 1, "Alice");
Obj2 := New User(2, "Bob");
```
更多:`Self`、`FindClass`、`ClassInfo`、`operator` 重载等见 `05_object_model.md`
---
## 扩展语法:矩阵/集合/过滤
### 矩阵初始化与访问
```tsl
M1 := zeros(10); // 一维数组
M2 := zeros(10,10); // 二维矩阵
M3 := rand(5,3); // 随机矩阵
M4 := eye(3); // 单位矩阵
Seq := 1->10; // 数列数组
```
```tsl
// 子矩阵与索引
Rows := MRows(M2,1);
Cols := MCols(M2,1);
Sub := M2[array(1,3), array(0,2)];
Col0 := M2[:,0];
```
### 矩阵查找
```tsl
A := Rand(10,10);
B := MFind(A, MCell>0.9);
```
### 集合运算
```tsl
A := array((1,2),(3,4));
B := array((1,2),(5,6));
C := A Union2 B;
D := A Intersect B;
E := A Minus B;
```
### 结果集过滤
```tsl
R1 := FilterIn(R, CodeArr, "Code");
Idx := FilterNotIn(R, CodeArr, "Code", false);
Sub := R[Idx, array("Code","V1")];
```
更多细节见 `06_extended_syntax.md`
---
## TS-SQLselect/insert/update/delete
```tsl
T := zeros(10, array("A","B"));
T[:,"A"] := 1->10;
V1 := select ["A"] from T end;
V2 := sselect ["A"] from T end;
V3 := vselect sumof(["A"]) from T end;
V4 := mselect * from T end;
update T set ["A"] = 100 where ["B"] = 4 end;
delete from T where ["A"] = 0 end;
insert into T insertfields(["A"],["B"]) values(1,2);
```
TS-SQL 语法细节与高级用法见 `06_extended_syntax.md`
---
## 运行时与性能语法要点
### 网格计算操作符 `#`
```tsl
R[i] := #CalcStock(B[i]) with array(pn_stock():B[i]) timeout 3000;
```
### 全局缓存
详见 `07_debug_and_profiler.md` 中的全局缓存管理与相关函数。
---
## 新一代语法概览
### 复数
```tsl
Z := 4+3j;
Z2 := complex(4,3);
```
### WeakRef 与算符重载
新一代语言支持 WeakRef 与对象算符重载,详见 `08_new_generation.md``05_object_model.md`