在 Flutter 中 class 调用之前的美元符号是什么意思?
What does the dollar sign mean when put before a class call in Flutter?
我是 Flutter/Dart 的初学者,我一直看到在调用 类 或方法之前出现美元符号,示例如下(在 Floor 包文档中找到) :
final database = await $FloorAppDatabase.databaseBuilder('app_database.db').build();
我搜索了很多,我能找到的 Dart 中美元符号的唯一含义是字符串插值,但似乎并非如此。
这不是 Flutter 或 Dart 约定——至少不是官方约定:the official Dart naming conventions document(截至 2020 年 10 月)未提及在标识符名称中使用 $
。
但是,我知道其他编程语言的生态系统确实使用 dollar-sign (Sigil) and I think that habit was inherited by the authors of the floor
database that you linked to. More specifically, in Java it's commonplace to use a $
prefix for type-names generated by tooling rather than being hand-written (such as ORM entity types, for example), and people using RxJS observables in JavaScript will use .
由于 $FloorDatabase
是一个 type-name,不是变量或 member-name,我将假设它是 picked-up 来自 [=42] 的习惯=]:
Java: The $
character should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.
所以在这种情况下,a clue is in the documentation you linked to:
Use the generated code. For obtaining an instance of the database, use the generated $FloorAppDatabase
class, which allows access to a database builder
所以这就是我的结论:它不是 Dart/Flutter 中的官方命名约定,而是 Java 中的,而且 floor
的作者似乎从 Java.
(我个人希望他们没有这样做,因为它不是一个特别有用的指标 - 如果一个类型是用工具而不是 hand-written 创建的,这对消费应用程序有什么关系?)
我是 Flutter/Dart 的初学者,我一直看到在调用 类 或方法之前出现美元符号,示例如下(在 Floor 包文档中找到) :
final database = await $FloorAppDatabase.databaseBuilder('app_database.db').build();
我搜索了很多,我能找到的 Dart 中美元符号的唯一含义是字符串插值,但似乎并非如此。
这不是 Flutter 或 Dart 约定——至少不是官方约定:the official Dart naming conventions document(截至 2020 年 10 月)未提及在标识符名称中使用 $
。
但是,我知道其他编程语言的生态系统确实使用 dollar-sign (Sigil) and I think that habit was inherited by the authors of the floor
database that you linked to. More specifically, in Java it's commonplace to use a $
prefix for type-names generated by tooling rather than being hand-written (such as ORM entity types, for example), and people using RxJS observables in JavaScript will use
由于 $FloorDatabase
是一个 type-name,不是变量或 member-name,我将假设它是 picked-up 来自 [=42] 的习惯=]:
Java: The
$
character should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.
所以在这种情况下,a clue is in the documentation you linked to:
Use the generated code. For obtaining an instance of the database, use the generated
$FloorAppDatabase
class, which allows access to a database builder
所以这就是我的结论:它不是 Dart/Flutter 中的官方命名约定,而是 Java 中的,而且 floor
的作者似乎从 Java.
(我个人希望他们没有这样做,因为它不是一个特别有用的指标 - 如果一个类型是用工具而不是 hand-written 创建的,这对消费应用程序有什么关系?)