Skip to main content
Version: 2.22

Types 类型

NASL 是一门静态类型语言,所有的值都有一个类型。

类型分类

PrimitiveTypes 原子类型

Boolean 布尔值

let b1: boolean = true;
let b2 = false;

Integer 整数

let i1: number = 36;
let i2 = -24;

Long 长整数

let l1: number = 13579246810;
let l2 = -24681013579;

Double 小数

let d1: number = 3.1415926;
let d2 = -3.14E-5;

Decimal 精确小数

BigDecimal decimal1 = BigDecimal("1.444444").setScale(2, BigDecimal.ROUND_DOWN);

String 字符串

let s1: string = 'Hello world!';
let s2 = 'He says, "I got some good news."';

Binary 二进制

let b1: ArrayBuffer = new Int16Array([1, 2, 3]).buffer;
let b2: Blob = new Blob('hello'.split(''));

Date 日期

let d1: string = '2020-12-02';

Time 时间

let t1: string = '10:30';
let t1: string = '10:30:45';

DateTime 日期时间

let z1: Date = new Date('2021-09-15T13:05:00Z');
let z2: Date = new Date('2021-09-15T21:05:00.188+08:00');

ReferenceTypes 引用类型

数据结构

{
"concept": "Structure",
"name": "StudentResult",
"typeParams": [],
"properties": [
{
"concept": "StructureProperty",
"name": "name",
"typeAnnotation": {
"concept": "TypeAnnotation",
"typeKind": "primitive",
"typeNamespace": "nasl.core",
"typeName": "String",
"typeArguments": null,
}
},
{
"concept": "StructureProperty",
"name": "age",
"typeAnnotation": {
"concept": "TypeAnnotation",
"typeKind": "primitive",
"typeNamespace": "nasl.core",
"typeName": "Integer",
"typeArguments": null,
}
},
{
"concept": "StructureProperty",
"name": "school",
"typeAnnotation": {
"concept": "TypeAnnotation",
"typeKind": "primitive",
"typeNamespace": "app.dataSources.defaultDS.entities",
"typeName": "School",
"typeArguments": null,
}
}
]
}

枚举

{
"concept": "Enum",
"name": "Color",
"label": "颜色",
"description": "表示颜色的枚举",
"enumItems": [{
"concept": "EnumItem",
"value": "RED",
"label": "红"
},
{
"concept": "EnumItem",
"value": "GREEN",
"label": "绿"
},
{
"concept": "EnumItem",
"value": "BLUE",
"label": "蓝"
}]
}

GenericTypes 泛型类型

这部分用户目前只能使用,无法自行定义。

列表

class List<T> {
length: number;
}

层级关系

如果 A 类型的值在任何地方都能被B类型的值赋值,A 类型就是 B 类型的超类型,反过来 B 类型就是 A 类型的子类型。

下图展示了 NASL 中常见类型的层级关系(连线开始为子类型、箭头末尾为超类型)。

graph BT; Boolean --> Any["Any<br>(java.lang.Object)"] Integer --> Any Long --> Any Double --> Any Decimal --> Any String --> Any Binary --> Any Date --> Any Time --> Any DateTime --> Any Email --> String URL --> String MathScore["MathScore<br>(CustomDataElement)"] --> Integer IDCard["IDCard<br>(CustomDataElement)"] --> String List --> Any Student["Student<br>(CustomStructure)"] --> Any Color["Color<br>(CustomEnum)"] --> Any Null --> Boolean Null --> MathScore Null --> Long Null --> Double Null --> Decimal Null --> Binary Null --> Date Null --> Time Null --> DateTime Null --> Email Null --> URL Null --> IDCard Null --> List Null --> Student Null --> Color
  • Any是所有类型的超类型,也称为顶级类型。
  • Null是所有类型的子类型,也称为底层类型。它有一个关键字字面量null

隐式转换

类型的隐式转换遵循以下两条规则:

  • 子类型赋值给超类型时进行隐式转换。
  • 原子类型按照下图方向进行隐式转换。
graph LR; Integer --> Long --> Double

暂不支持用户自定义隐式转换策略。