Types 类型
NASL 是一门静态类型语言,所有的值都有一个类型。
类型分类
PrimitiveTypes 原子类型
Boolean 布尔值
- TypeScript
- Java
let b1: boolean = true;
let b2 = false;
boolean b1 = true;
boolean b2 = false;
Integer 整数
- TypeScript
- Java
let i1: number = 36;
let i2 = -24;
int i1 = 36;
int i2 = -24;
Long 长整数
- TypeScript
- Java
let l1: number = 13579246810;
let l2 = -24681013579;
long l1 = 13579246810;
long l2 = -24681013579L;
Double 小数
- TypeScript
- Java
let d1: number = 3.1415926;
let d2 = -3.14E-5;
double d1 = 3.1415926;
double d2 = -3.14E-5;
Decimal 精确小数
- 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.\"";
Binary 二进制
- TypeScript
- Java
let b1: ArrayBuffer = new Int16Array([1, 2, 3]).buffer;
let b2: Blob = new Blob('hello'.split(''));
byte b1 = 127;
byte[] b2 = new byte[4];
https://zhuanlan.zhihu.com/p/147664305?from_voters_page=true
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
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
暂不支持用户自定义隐式转换策略。