RFC-0052:类型别名和新类型

RFC-0052:类型重名和新类型
状态已接受
区域
  • FIDL
说明

此提案旨在正式演变 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;

但这不会(等式右侧的部分类型引用):

alias SmallVec = vector:SMALL_NUM;

虽然 FIDL 目前支持使用 using 别名进行部分类型引用(如 SmallVec 示例所示),但此功能将被移除。弃用此 API 的原因有三点:

  1. 尚未针对 FIDL 完全定义或审核类型泛型。在未来支持类型泛型的情况下,该语言将能够以更好、更正式的方式描述可参数化的类型和类型别名。
  2. 我们尚未就如何将泛型转换为不支持泛型的语言(例如 Go 1、C)做出正式决定。
  3. 当前语法会导致在某些情况下,可能隐式需要类型形参才能创建完整的类型。此外,如何解析和参数化嵌套的部分类型别名(即 using SmallVecOfVecs = vector<vector:SMALL_NUM>:SMALL_NUM)的具体细节尚不明确。

不支持 protocol 的别名,因为 protocol 无法在 FIDL 或绑定语言中完全归类为类型。我们的目标是在协议变得更类似于类型(即 client_end:ProtocolNameserver_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_tzx_paddr_t 由相同的底层 uintptr_t 类型表示,但具有不同的(但容易混淆)含义。

借助新类型,我们可以在类型系统中表达这些语义差异。在强类型语言中,这意味着可以在编译时通过类型检查和/或静态分析捕获逻辑 bug。

线头格式不会发生任何变化,也不会产生任何费用。

语法

newtype-declaration = ( attribute-list ), "type", IDENTIFIER, "=", type-constructor;

此语法是根据 RFC-0050:语法改进中指定的顶级类型的引入而选择的。

由于新类型应转换为绑定中的具体类型,因此 type-constructor 应解析为完全形成的类型引用。可能如下所示:

type MyBytes = vector<uint8>:MAX;

系统不支持新类型的 protocol,因为 protocol 无法在 FIDL 或绑定语言中完全归类为一种类型。此外,目前在生成的绑定中,对 protocol 派生实体使用命名类型语义没有强大的用例。

类型特征和运算符

在没有新类型的情况下,可以通过 FIDL 中新的单字段 struct 类型大致实现新类型的效果。例如:

struct MyBytes {
    vector<uint8>:MAX value;
};

不过,新类型会向后端和绑定指明更窄的语义空间,并允许它们生成特定于语言的功能,这些功能可能会映射到原生语言功能,或使新类型易于使用。例如:

语言 说明
C 遗憾的是,C 的类型系统没有良好的表示法。回退到 typedef
C++ 新类型可以转换为 class,该类型可能会公开以下基本类型的功能:(1) 显式转换构造函数、(2) 显式赋值运算符、(3) 算术和按位运算,以及 (4) 其他运算符([]*-> 等)
Rust 新类型可以转换为单例结构体,该结构体派生出底层类型实现的 From<UnderlyingType>Into<UnderlyingType>HashPartialEqCopyClonestd::ops::* 等 trait。
Dart 与 Dart 团队的对话表明,目前无法将新类型语义映射到该语言,但我们曾讨论过如何支持此类用例 3
新类型可以降级为类型别名,具体取决于 #65 [^2]。
Go 新类型直接映射到类型定义。
type <NewType> <UnderlyingType>

底层类型的一组固有功能从此被称为类型特征(该概念尚未在该语言中正式定义)。

除非/直到在 FIDL 中定义了类型 trait,否则新类型默认应继承底层类型的 trait。这样一来,新类型仍可像基础类型一样发挥作用,而无需解封装基础类型,也不会破坏新类型带来的类型安全优势。将来,能够定义新类型的自定义 trait 行为可能会很有用,而此设计不会阻止将来朝着这样的方向演变。不过,默认的 trait 继承确实会使向可选继承 trait 的迁移路径成为一项重大的破坏性更改。

示例

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...

  1. Go 将在不久的将来支持泛型。 

  2. 待 Dart 引入非函数类型的 typedef (#65)。 

  3. 请参阅 Dart 问题 #42 中的评论。