Types 类型
NASL 是一门静态类型语言,所有的值都有一个类型。
类型分类
PrimitiveTypes 原子类型
Boolean 布尔值
- TypeScript
- Java
let b1: boolean = true;
let b2 = false;
boolean b1 = true;
boolean b2 = false;
Integer 整数
NASL 中没有短整型和长整型的区分,所有整数都是长整型。
- TypeScript
- Java
let i1: number = 36;
let i2 = -24;
long i1 = 36L;
long i2 = -24L;
Decimal 小数
NASL 中没有双精度类型,所有小数统一用精确小数。
- Java
BigDecimal decimal1 = BigDecimal("1.444444").setScale(2, BigDecimal.ROUND_DOWN);
String 字符串
- TypeScript
- Java
let s1: string = 'Hello world!';
let s2 = 'He says, "I got some good news."';
String s1 = "Hello world!";
String s2 = "He says, \"I got some good news.\"";
Date 日期
- TypeScript
- Java
let d1: string = '2020-12-02';
// rt.jar
LocalDate d1 = LocalDate.of(2020, 12, 2);
LocalDate d2 = LocalDate.parse("2020-12-02");
Time 时间
- TypeScript
- Java
let t1: string = '10:30';
let t1: string = '10:30:45';
// rt.jar
LocalTime t1 = LocalTime.of(10, 30, 45);
LocalTime t2 = LocalTime.parse("10:30:45");
DateTime 日期时间
- TypeScript
- Java
let z1: Date = new Date('2021-09-15T13:05:00Z');
let z2: Date = new Date('2021-09-15T21:05:00.188+08:00');
// rt.jar
ZonedDateTime z1 = ZonedDateTime.parse("2021-09-15T13:05:00Z");
ZonedDateTime z2 = ZonedDateTime.of(2021, 9, 15, 21, 5, 0).atZone(ZoneId.of(ZoneId.systemDefault()));
ReferenceTypes 引用类型
数据结构
- JSON
- TypeScript
- Java
{
"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
}
}
]
}
class Student {
name: string;
age: number;
school: School;
}
class Student {
String name;
Integer age;
School school;
}
枚举
- JSON
- TypeScript
- Java
{
"concept": "Enum",
"name": "Color",
"label": "颜色",
"description": "表示颜色的枚举",
"enumItems": [
{
"concept": "EnumItem",
"value": "RED",
"label": "红"
},
{
"concept": "EnumItem",
"value": "GREEN",
"label": "绿"
},
{
"concept": "EnumItem",
"value": "BLUE",
"label": "蓝"
}
]
}
enum Color {
RED = '红',
GREEN = '绿',
BLUE = '蓝',
}
enum Color {
RED("红"), GREEN("绿"), BLUE("蓝");
private final String color;
private Color(String color) {
this.color = color;
}
}
GenericTypes 泛型类型
这部分用户目前只能使用,无法自行定义。
列表
- TypeScript
- Java
class List<T> {
length: number;
}
java.util.List
层级关系
如果 A 类型的值在任何地方都能被 B 类型的值赋值,A 类型就是 B 类型的超类型,反过来 B 类型就是 A 类型的子类型。
下图展示了 NASL 中常见类型的层级关系(连线开始为子类型、箭头末尾为超类型)。
graph BT;
Boolean --> Any["Any<br>(java.lang.Object)"]
Integer --> Any
Decimal --> Any
String --> Any
Date --> Any
Time --> Any
DateTime --> Any
List --> Any
Student["Student<br>(CustomStructure)"] --> Any
Color["Color<br>(CustomEnum)"] --> Any
Null --> Boolean
Null --> Integer
Null --> Decimal
Null --> Date
Null --> Time
Null --> DateTime
Null --> List
Null --> Student
Null --> Color
Any
是所有类型的超类型,也称为顶级类型。Null
是所有类型的子类型,也称为底层类型。它有一个关键字字面量null
。
隐式转换
类型的隐式转换遵循以下两条规则:
- 子类型赋值给超类型时进行隐式转换。
- 原子类型按照下图方向进行隐式转换。
graph LR;
Integer --> Decimal
不支持用户自定义隐式转换策略。