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