RFC-0052:类型别名和新类型 | |
---|---|
状态 | 已接受 |
领域 |
|
说明 | 此方案旨在正式改进 FIDL 的类型别名和新的类型声明机制及其对绑定的影响。具体而言,这提议优化类型别名语法、为绑定提供类型别名,以及一项称为新类型的新功能。 |
作者 | |
提交日期(年-月-日) | 2020-01-07 |
审核日期(年-月-日) | 2020-01-23 |
“用其他名字给我打电话”
总结
RFC-0019 的部分内容在 FIDL 中引入了类型别名的概念。此方案旨在正式改进 FIDL 的类型别名和新的类型声明机制及其对绑定的影响。更具体地说,这提出了优化类型别名语法、向绑定提供类型别名的功能,以及一项称为新类型的新功能。
设计初衷
类型别名现已是 FIDL 中的一项功能,此 RFC 旨在扩大其范围和实用性。使用类型别名后,声明会对绑定产生影响,因此有助于 API 名称转换。
除了类型别名之外,此 RFC 还引入了一个姊妹概念,即新的类型声明。与别名非常相似,新类型会在本地范围中声明一个封装其他类型的新名称,但它们会被视为与类型检查工具(命名的类型语义)不同。这样,FIDL 作者就可以更好地利用其绑定语言的类型系统。
设计
类型别名
如需了解类型别名的完整上下文,RFC-0019(提出了当前使用样式别名的建议)突出显示了原始设计、动机和优先级。此 RFC 旨在为在语言绑定中公开类型别名铺平道路,并优化 RFC-0019 中的一些语言语法决策。与使用别名非常相似,新的类型别名纯粹是名义,不会影响 ABI。本部分将主要概述原始功能的更改和改进。
语法
alias-declaration = ( attribute-list ) , "alias" , IDENTIFIER , "=" , type-constructor;
type-constructor
应解析为当前范围内存在的完整格式类型引用。换言之,对于 vector
这样的类型,必须同时包含内部类型和尺寸限制条件。
例如,以下代码是有效的:
alias SmallBytes = vector<uint8>:SMALL_NUM;
但这不会(equals 右侧的部分类型引用):
alias SmallVec = vector:SMALL_NUM;
虽然 FIDL 目前支持使用 using
别名的部分类型引用(如 SmallVec
示例所示),但此功能将被移除。弃用此产品的原因有三个:
- 类型泛型尚未针对 FIDL 进行完整定义或审核。在未来存在类型泛型的情况下,语言将能以更好、更正式的方式来描述可参数化的类型和类型别名。
- FIDL 语言尚未就如何将泛型转换为不支持泛型的语言(即 Go 1、C)做出正式决定。
- 当前语法会创造一些场景,在这些场景中,系统可能隐式需要类型参数,才能创建完全格式的类型。此外,如何解析和参数化嵌套的部分类型别名(即
using SmallVecOfVecs = vector<vector:SMALL_NUM>:SMALL_NUM
)的具体情况也不明确。
不支持 protocol
的别名,因为无论是使用 FIDL 还是绑定语言,protocol
都无法完全归类为类型。目标是在协议变得更像类型(即 client_end:ProtocolName
和 server_end:ProtocolName
)时重新考虑这一决定。
示例
语言 | 代码 |
---|---|
FIDL | alias SmallBytes = vector<uint8>:SMALL_NUM; |
C | (在简单的 C 绑定中不存在向量)typedef small_bytes uint8_t*; |
C++ | using SmallBytes = std::vector<uint8_t>; |
Rust | type SmallBytes = Vec<u8>; |
Dart 2 | typedef SmallBytes = List<int>; |
Go | type SmallBytes = []uint8; |
新类型
新类型是封装底层类型的类型,但被视为与语言类型系统中的基础类型不同。如果两个值具有相同的字号和形状(可能还包括特征),但具有不同的语义含义,则此方法可能很有用。例如,zx_vaddr_t
和 zx_paddr_t
由相同的底层 uintptr_t
类型表示,但其含义不同(但很容易混淆)。
新类型允许在类型系统中表示这些语义差异。在强类型语言中,这意味着可以在编译时通过类型检查和/或静态分析来发现逻辑 bug。
如果采用传输格式,这不会发生任何变化,也不会产生费用。
语法
newtype-declaration = ( attribute-list ), "type", IDENTIFIER, "=", type-constructor;
我们选择此语法是为了配合“RFC-0050:语法修订”中指定的顶级类型的引入。
由于新类型应在绑定中转换为具体类型,因此 type-constructor
应解析为完全格式的类型引用。它可能如下所示:
type MyBytes = vector<uint8>:MAX;
将不支持新的 protocol
类型,因为无论是使用 FIDL 还是绑定语言,protocol
都无法完全归类为某种类型。此外,在生成的绑定中,对于 protocol
派生的实体,目前没有令人信服的用例。
类型特征和运算符
如果没有新类型,新类型的效果大致可以通过 FIDL 中的新单字段 struct
类型来实现。例如:
struct MyBytes {
vector<uint8>:MAX value;
};
但是,新类型会向后端指示并绑定更窄的语义空间,并允许它们生成特定于语言的特征,这些特征可能会映射到原生语言特征或使新类型符合工效学使用要求。例如:
语言 | 说明 |
---|---|
C | 遗憾的是,C 的类型系统没有良好的表示形式。回退到 typedef 。 |
C++ | 新类型可转换为 class ,它可从基础类型公开以下功能:(1) 显式转换构造函数,(2) 显式赋值运算符,(3) 算术和按位运算,以及 (4) 其他运算符([] 、* 、-> 等) |
Rust | 新类型可以转换为单例结构体,该结构体会派生 From<UnderlyingType> 、Into<UnderlyingType> 、Hash 、PartialEq 、Copy 、Clone 、std::ops::* 等特征,这些特征由底层类型实现。 |
Dart | 通过与 Dart 团队的对话,目前还没有办法将新的类型语义映射到语言,但一直讨论支持此类用例 3。 新类型可以降级为类型别名,待定 #65 [^2]。 |
Go | 新类型会直接映射到类型定义。type <NewType> <UnderlyingType> |
基础类型的固有功能集因此称为“类型特征”(一个尚未在语言中正式定义的概念)。
除非/之前在 FIDL 中定义类型特征,否则新类型应默认继承基础类型的特征。这样一来,新类型仍然会非常有用,而且与底层类型的作用相同,无需解封底层类型,也无需撤消新类型的类型安全优势。如果能够定义新类型的自定义特征行为,未来可能会很有用,而且这种设计不会阻止未来朝着此类路径演变。但是,默认情况下,特征继承会使用户选择接受特征继承的迁移路径成为一项重大的破坏性更改。
示例
C++
// This example uses C++20 concepts for readability but this can be translated to a
// template approach in C++14.
template<typename T>
concept Addable = requires(T a) {
{ a + a } -> T;
};
template<typename T>
concept Multipliable = requires(T a) {
{ a * a } -> T;
};
template <typename T>
concept Copyable = std::is_copy_constructible_v<T> || std::is_copy_assignable_v<T>;
template <typename T>
concept Movable = std::is_move_constructible_v<T> || std::is_move_assignable_v<T>;
class MyNumber {
public:
using UnderlyingType = uint32_t;
explicit MyNumber() = default;
explicit MyNumber(const UnderlyingType& value)
requires Copyable<UnderlyingType>
: value_(value) {}
explicit MyNumber(UnderlyingType&& value) requires Movable<UnderlyingType>
: value_(std::move(value)) {}
MyNumber& operator=(const MyNumber&) = default;
MyNumber& operator=(MyNumber&&) = default;
[[nodiscard]] MyNumber operator+(const MyNumber& other) const
requires Addable<UnderlyingType> && Copyable<UnderlyingType> {
return MyNumber(value_ + other.value_);
}
[[nodiscard]] MyNumber operator+(MyNumber&& other)
requires Addable<UnderlyingType> && Movable<UnderlyingType> {
return MyNumber(value_ + other.value_);
}
[[nodiscard]] MyNumber operator*(const MyNumber& other) const
requires Multipliable<UnderlyingType> && Copyable<UnderlyingType> {
return MyNumber(value_ * other.value_);
}
[[nodiscard]] MyNumber operator*(MyNumber&& other)
requires Multipliable<UnderlyingType> {
return MyNumber(value_ + other.value_);
}
// ...other operators defined here...
[[nodiscard]] UnderlyingType value() const
requires Copyable<UnderlyingType> {
return value_;
}
UnderlyingType take_value() requires Movable<UnderlyingType> {
return std::move(value_);
}
private:
UnderlyingType value_;
};
Rust
#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
struct MyNumber(u32);
impl From<u32> for MyNumber {
fn from(value: u32) -> Self {
MyNumber(value)
}
}
impl Into<u32> for MyNumber {
fn into(self) -> u32 {
self.0
}
}
impl Add for MyNumber {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl Mul for MyNumber {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
Self(self.0 * rhs.0)
}
}
// ...implement other traits here...