TypeDeclaration["Product",name,<|field1type1,field2type2,…|>]
表示具有指定字段的 product 类型的声明.
TypeDeclaration["Abstract",name]
表示抽象类型 name 的声明.
TypeDeclaration["Alias",name,targetType]
表示类型 name 的声明,使用的是 targetType 的内部表示.
TypeDeclaration["Macro",name,targetType]
表示一个声明,指定 name 的所有实例应被 targetType 替换.
TypeDeclaration
TypeDeclaration["Product",name,<|field1type1,field2type2,…|>]
表示具有指定字段的 product 类型的声明.
TypeDeclaration["Abstract",name]
表示抽象类型 name 的声明.
TypeDeclaration["Alias",name,targetType]
表示类型 name 的声明,使用的是 targetType 的内部表示.
TypeDeclaration["Macro",name,targetType]
表示一个声明,指定 name 的所有实例应被 targetType 替换.
更多信息和选项
- TypeDeclaration 是声明的符号表示,不会自行进行计算.
- TypeDeclaration 可在 CompilerEnvironmentAppendTo 的内部使用,也可以在 FunctionCompile 等函数的第一个参数中使用.
- 支持以下类型的声明:
-
"Product" 含有几个字段的类型;类似于 C 中的结构体 (struct) "Abstract" 具体类型可以是某个类型的类别 (class) 的实例的抽象类型 "Alias" 内部实现与另一种类型相同的类型 "Macro" 适用于所有类型的替换规则 - 当与 C 兼容程序对接时,以下类型声明是等效的:
- 可用 prod["field"] 访问和设置 product 类型实例 prod 的字段.
- 支持以下选项:
-
"AbstractTypes" {} 含有所声明类型的抽象类型 "Creator" None 调用以创建声明类型的实例的函数 "MemoryManaged" Automatic 是否自动释放未引用的实例 "Operations" {} 可用于所声明类型的运算 "ReferenceSemantics" Automatic 是否在内部用指针表示类型 - 默认情况下,"Product" 声明含有 "MemoryManaged"True 和 "ReferenceSemantics"True 设置.
- 对于 "Product" 声明,"ReferenceSemantics" 只能被设为 True.
- 对于具有引用语义的 "Product" 声明,"MemoryManaged" 只能被设为 True.
- "Creator" 只能为 "Product" 声明设置.
- "Operations" 只能为 "Product" 和 "Abstract" 声明设置.
- 不能为 "Macro" 声明设置 "AbstractTypes".
- 为了与 C 结构体兼容,product 类型通常必须设置为 "MemoryManaged",而 "ReferenceSemantics" 通常必须设置为 False.
- 对作为 product 类型实例的对象 prod 定义了以下方法:
-
prod["field"] 获取 "field" 的值 prod["field"]=val 设置 "field" 的值 DeleteObject[prod] 释放 prod (只在 prod 不是内存管理的情况下可用)
范例
打开所有单元 关闭所有单元基本范例 (2)
将 "fooInteger" 的声明表示为 "MachineInteger" 的宏类型:
dec = TypeDeclaration["Macro", "fooInteger", "MachineInteger"];cf = FunctionCompile[dec, Function[Typed[arg, "fooInteger"], arg + 2]]cf[5]全局声明 "fooInteger" 为 "MachineInteger" 的宏:
CompilerEnvironmentAppendTo[{
TypeDeclaration["Macro", "fooInteger", "MachineInteger"]
}];cf = FunctionCompile[Function[Typed[arg, "fooInteger"], arg + 2]]cf[5]$CompilerEnvironment = CreateCompilerEnvironment[];范围 (6)
"Product" (2)
dec = TypeDeclaration["Product", "simpleProd", <|"field1" -> "MachineInteger", "field2" -> "Boolean"|>];编译一个实例化 product 类型的函数,然后提取一个字段:
cf = FunctionCompile[dec, Function[{},
With[{prod = CreateTypeInstance["simpleProd", <|"field1" -> 12, "field2" -> True|>]},
prod["field1"]]
]]cf[]dec = TypeDeclaration["Product", "polyProd"::[T], <|"field1" -> T, "field2" -> "Boolean"|>];编译一个接受机器整数的函数,并使用它来创建上面的 product 类型:
cf = FunctionCompile[dec,
Function[{Typed[n, "MachineInteger"]},
Module[{prod = CreateTypeInstance["polyProd", <|"field1" -> n, "field2" -> True|>]},
prod["field1"] + 1
]
]
]cf[42]"Abstract" (1)
dec1 = TypeDeclaration["Abstract", "simpleAbstract"];dec2 = TypeDeclaration["Product", "abstractInstance1", <|"field1" -> "MachineInteger", "field2" -> "Boolean"|>, "AbstractTypes" -> {"simpleAbstract"}];dec3 = TypeDeclaration["Product", "abstractInstance2", <|"field1" -> "MachineInteger", "field2" -> "Real64"|>];dec4 = FunctionDeclaration[fieldExtractor,
Typed[ForAllType[t, t∈"simpleAbstract", {t} -> "MachineInteger"]]@
Function[arg, arg["field1"]]
];cf = FunctionCompile[{dec1, dec2, dec3, dec4}, Function[{},
fieldExtractor[CreateTypeInstance["abstractInstance1", <|"field1" -> 12, "field2" -> True|>]]
]]cf[]FunctionCompile[Function[{},
fieldExtractor[CreateTypeInstance["abstractInstance2", <|"field1" -> 12, "field2" -> 3.4|>]]
]]"Alias" (1)
dec = TypeDeclaration["Alias", "aliasString", "String"];funcDec = FunctionDeclaration[StringLength,
Typed[{"aliasString"} -> "MachineInteger"]@
Function[arg, 12]
];编译一个程序,通过 bitcasting 实例化别名,然后用来对函数进行计算:
cf = FunctionCompile[{dec, funcDec}, Function[Typed[str, "String"],
StringLength[Cast[str, "aliasString", "BitCast"]]]]cf["hello"]"Macro" (2)
dec = TypeDeclaration["Macro", "macroString", "String"];cf = FunctionCompile[{dec}, Function[Typed[expr, "macroString"], StringLength[expr]]]cf["hello"]宏不是类型,而是在解析时应用于类型的替换规则. 因此,一个宏和它所指代的类型不可能对同一个函数有不同的定义:
dec = TypeDeclaration["Macro", "macroString", "String"];funcDec = FunctionDeclaration[StringLength, Typed[{"macroString"} -> "MachineInteger"]@
Function[arg, 12]
];FunctionCompile[{dec, funcDec},
Function[Typed[arg, "macroString"], StringLength[arg]]]
选项 (11)
"AbstractTypes" (2)
absDec = TypeDeclaration["Abstract", "simpleAbstract"];prodDec = TypeDeclaration["Product", "abstractInstance", <|"field1" -> "MachineInteger", "field2" -> "Boolean"|>, "AbstractTypes" -> {"simpleAbstract"}];funcDec = FunctionDeclaration[abstractEater,
Typed[ForAllType[t, t∈"simpleAbstract", {t} -> "MachineInteger"]]@
Function[arg, 42]];编译该函数,显示它接受是抽象类型成员的 product 类型:
cf = FunctionCompile[{absDec, prodDec, funcDec},
Function[{}, With[{
obj = CreateTypeInstance["abstractInstance", <|"field1" -> 12, "field2" -> False|>]},
abstractEater[obj]]
]]cf[]superDec = TypeDeclaration["Abstract", "superType"];subDec = TypeDeclaration["Abstract", "subType", "AbstractTypes" -> {"superType"}];prodDec = TypeDeclaration["Product", "abstractInstance", <|"field1" -> "MachineInteger", "field2" -> "Boolean"|>, "AbstractTypes" -> {"subType"}];表示一个接受抽象类型 "superType" 的任何实例的函数:
funcDec = FunctionDeclaration[abstractEater,
Typed[ForAllType[t, t∈"superType", {t} -> "MachineInteger"]]@
Function[arg, 42]];编译该函数,显示它接受通过 "subType" 继承 "superType" 成员资格的 product 类型:
cf = FunctionCompile[{superDec, subDec, prodDec, funcDec},
Function[{}, With[{
obj = CreateTypeInstance["abstractInstance", <|"field1" -> 12, "field2" -> False|>]},
abstractEater[obj]]
]]cf[]"Creator" (1)
typeDec = TypeDeclaration["Product", "TestType", <|"value" -> "MachineInteger"|>, "Creator" -> CreateTestType];createDec = FunctionDeclaration[CreateTestType,
Typed[{} -> "TestType"]@
Function[{}, CreateTypeInstance["TestType", <|"value" -> 10|>]]];编译一个使用 CreateTypeInstance 的函数,并调用 "Creator" 函数:
cf = FunctionCompile[{absDec, prodDec, funcDec},
Function[{}, With[{
obj = CreateTypeInstance["abstractInstance", <|"field1" -> 12, "field2" -> False|>]},
abstractEater[obj]]
]]cf[]"MemoryManaged" (3)
unmanaged = TypeDeclaration["Product", "unmanagedType", <|"field" -> "MachineInteger"|>, "MemoryManaged" -> False];cf = FunctionCompile[{superDec, subDec, prodDec, funcDec},
Function[{}, With[{
obj = CreateTypeInstance["abstractInstance", <|"field1" -> 12, "field2" -> False|>]},
abstractEater[obj]]
]]With[{before = MemoryInUse[]},
Do[cf[], 1000];
MemoryInUse[] - before]managed = TypeDeclaration["Product", "managedType", <|"field" -> "MachineInteger"|>];cf = FunctionCompile[{typeDec, createDec}, Function[{},
With[{obj = CreateTypeInstance["TestType"]},
obj["value"]
]
]]With[{before = MemoryInUse[]},
Do[cf2[], 1000];
MemoryInUse[] - before]unmanaged = TypeDeclaration["Product", "unmanagedType", <|"field" -> "MachineInteger"|>, "MemoryManaged" -> False];编译一个使用该类型的函数并用,并用 DeleteObject 释放它:
cf = FunctionCompile[unmanaged, Function[{},
Module[{obj, out},
obj = obj = CreateTypeInstance["unmanagedType", <|"field" -> 12|>];
out = obj["field"];
DeleteObject[obj];
out
]
]]With[{before = MemoryInUse[]},
Do[cf[], 1000];
MemoryInUse[] - before]由内存管理的 product 类型包括引用计数,并且与外部程序不兼容. 将 product 型传进和传出库函数时,应禁用内存管理.
Needs["CCompilerDriver`"];
CreateLibrary["
#include \"dllexport.h\"
typedef struct {
int f1;
float f2;
} foo;
DLLEXPORT int getIntFieldRef(foo* arg) {
return arg->f1;
}", "memoryManagementExample"];用 TypeDeclaration 表示兼容类型:
fooTypeDec = TypeDeclaration["Product", "foo", <|"f1" -> "CInt", "f2" -> "CFloat"|>, "MemoryManaged" -> False];注意已编译类型 "foo" 默认是引用类型,相当于 C 中的 foo*.
cf = FunctionCompile[{
fooTypeDec, LibraryFunctionDeclaration["getIntFieldRef", "memoryManagementExample", {"foo"} -> "CInt"]
},
Function[{},
With[{foo = CreateTypeInstance["foo", <|"f1" -> Typed[42, "CInt"], "f2" -> Typed[3.1, "CFloat"]|>]},
LibraryFunction["getIntFieldRef"][foo]
]
]
]cf[]"Operations" (3)
dec1 = TypeDeclaration["Product", "simpleValue", <|"value" -> "MachineInteger"|>, "Operations" -> {"Increment"}, "AbstractTypes" -> {"DataStructures"}];dec2 = OperationDeclaration["simpleValue", "Increment", Typed[{"simpleValue"} -> "MachineInteger"]@Function[{arg},
arg["value"]++
]];编译一个函数,创建该类型的实例及另一实例,调用 "Increment" 运算:
funs = FunctionCompile[{dec1, dec2}, <|"create" -> Function[{}, CreateTypeInstance["simpleValue", <|"value" -> 0|>]],
"increment" -> Function[{Typed[ arg, "simpleValue"]}, arg["Increment"]]
|>]obj = funs["create"][];funs["increment"][obj]funs["increment"][obj]也可以为抽象类型定义运算. 在这种情况下,它们可用于实现抽象类型的任何类型:
absDec = TypeDeclaration["Abstract", "incrementer", "Operations" -> {"Increment"}];prodDec = TypeDeclaration["Product", "testType", <|"value" -> "MachineInteger"|>, "AbstractTypes" -> {"incrementer", "DataStructures"}];decOp = OperationDeclaration["testType", "Increment", Typed[ {"testType"} -> "MachineInteger"]@Function[{arg},
arg["value"]++
]];编译一个函数,创建该类型的实例及另一实例,调用 "Increment" 运算:
funs = FunctionCompile[{absDec, prodDec, decOp}, <|"create" -> Function[{}, CreateTypeInstance["testType", <|"value" -> 0|>]],
"increment" -> Function[{Typed[ arg, "testType"]}, arg["Increment"]]
|>]obj = funs["create"][];funs["increment"][obj]funs["increment"][obj]absDec = TypeDeclaration["Abstract", "incrementer", "Operations" -> {"Increment" -> increment}];运算声明;它是用 ForAllType 多态编写的:
decOp = FunctionDeclaration[increment,
Typed[ForAllType[v, {v} -> "MachineInteger"]]@Function[{arg},
arg["value"]++
]];prodDec = TypeDeclaration["Product", "testType", <|"value" -> "MachineInteger"|>, "AbstractTypes" -> {"incrementer", "DataStructures"}];编译一个函数,创建该类型的实例及另一实例,调用 "Increment" 运算:
funs = FunctionCompile[{absDec, prodDec, decOp}, <|"create" -> Function[{}, CreateTypeInstance["testType", <|"value" -> 0|>]],
"increment" -> Function[{Typed[ arg, "testType"]}, arg["Increment"]]
|>]obj = funs["create"][];funs["increment"][obj]funs["increment"][obj]也可以用其他方式实现运算. 由于它是用实际类型以更窄的方式声明的,因此它会覆盖抽象类型给出的实现:
decOp1 = OperationDeclaration["testType", "Increment", Typed[{"testType"} -> "MachineInteger"]@Function[{arg},
arg["value"] += 2
]];编译一个函数,创建该类型的实例及另一实例,调用 "Increment" 运算:
funs = FunctionCompile[{absDec, prodDec, decOp, decOp1}, <|"create" -> Function[{}, CreateTypeInstance["testType", <|"value" -> 0|>]],
"increment" -> Function[{Typed[ arg, "testType"]}, arg["Increment"]]
|>]obj = funs["create"][];funs["increment"][obj]"ReferenceSemantics" (2)
valueTypeDec = TypeDeclaration["Product", "valueType", <|"field" -> "MachineInteger"|>, "ReferenceSemantics" -> False];cf = FunctionCompile[valueTypeDec, Function[{},
With[{obj = CreateTypeInstance["valueType", <|"field" -> 12|>]},
obj["field"]
]
]]cf[]Needs["CCompilerDriver`"];
CreateLibrary["
#include \"dllexport.h\"
typedef struct {
int f1;
float f2;
} foo;
DLLEXPORT int getIntField(foo arg) {
return arg.f1;
}", "valueTypeExample"];用 TypeDeclaration 表示兼容类型:
fooTypeDec = TypeDeclaration["Product", "foo", <|"f1" -> "CInt", "f2" -> "CFloat"|>, "ReferenceSemantics" -> False];cf = FunctionCompile[{
fooTypeDec, LibraryFunctionDeclaration["getIntField", "valueTypeExample", {"foo"} -> "CInt"]
},
Function[{},
With[{foo = CreateTypeInstance["foo", <|"f1" -> Typed[42, "CInt"], "f2" -> Typed[3.1, "CFloat"]|>]},
LibraryFunction["getIntField"][foo]
]
]
]cf[]应用 (1)
C 标准库包含一个名为 ldiv 的函数,用于计算两个整数的商和余数. 它返回一个声明为以下内容的对象:
typedef struct {
long quot; /* quotient */
long rem; /* remainder */
} ldiv_t;
divtDec = TypeDeclaration["Product", "CLDivT", <|"quot" -> "CLong", "rem" -> "CLong"|>, "ReferenceSemantics" -> False];funcDec = LibraryFunctionDeclaration["ldiv", Automatic, {"CLong", "CLong"} -> "CLDivT"];quotRem = FunctionCompile[{divtDec, funcDec},
Function[{Typed[n, "CLong"], Typed[d, "CLong"]},
With[{divt = LibraryFunction["ldiv"][n, d]},
{divt["quot"], divt["rem"]}]
]
]quotRem[7, 2]可能存在的问题 (1)
宏不是类型,而是在解析时应用于类型的替换规则. 因此,一个宏和它所指代的类型不可能对同一个函数有不同的定义:
dec = TypeDeclaration["Macro", "macroString", "String"];funcDec = FunctionDeclaration[StringLength, Typed[{"macroString"} -> "MachineInteger"]@
Function[arg, 12]
];FunctionCompile[{dec, funcDec},
Function[Typed[arg, "macroString"], StringLength[arg]]]
技术笔记
文本
Wolfram Research (2022),TypeDeclaration,Wolfram 语言函数,https://reference.wolfram.com/language/ref/TypeDeclaration.html.
CMS
Wolfram 语言. 2022. "TypeDeclaration." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/ref/TypeDeclaration.html.
APA
Wolfram 语言. (2022). TypeDeclaration. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/TypeDeclaration.html 年
BibTeX
@misc{reference.wolfram_2026_typedeclaration, author="Wolfram Research", title="{TypeDeclaration}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/TypeDeclaration.html}", note=[Accessed: 04-November-2026]}
BibLaTeX
@online{reference.wolfram_2026_typedeclaration, organization={Wolfram Research}, title={TypeDeclaration}, year={2022}, url={https://reference.wolfram.com/language/ref/TypeDeclaration.html}, note=[Accessed: 04-November-2026]}