在 M - Text.Type 或类型文本中声明数据类型的更好语法?

Better syntax to declare data types in M - Text.Type or type text?

对于 Power Query/PowerBI 中的 M,我应该使用哪种语法来设置数据类型?
A) type text(或type logicaltype date等)
B) Text.Type(或Logical.TypeDate.Type等)

既然选项 B 存在,是否有任何理由使用选项 A 语法?我尝试阅读 Power Query M language specification 的第 5 章,但找不到明确的答案。

这是一个使用 Table.AddColumn 的示例(尽管数据类型无处不在):

let
   OldTable = #table({"Col1"},{{"This column"}}),
   fMyFunc = (paramText as text ) as text => let returnText = paramText & "_new" in returnText,
   NewTable = Table.AddColumn(OldTable, "NewCol", each "Sample", Text.Type),
   NewerTable = Table.AddColumn(NewTable, "NewerCol", each fMyFunc([NewCol]), Text.Type)
in
   NewerTable

我相信引入选项 B 只是为了标准化类型定义;例如,有 Int64.Type 但没有 type Int64。因此,我的问题的答案可能是 "It doesn't matter at all." 但是,如果一个选项似乎是未来的共识,我宁愿现在就开始在我的代码中保持一致。

我同意这并不重要,除非从风格的角度来看。

正如您所提到的,像 Int64.Type 这样的非基本类型不能像 type text 那样写成 type Int64,所以如果您想在两者之间保持风格一致原始类型和非原始类型,那么你需要选项 B.


您链接的文档第 48 页和第 49 页列出的原始类型是:

  • type null, which classifies the null value
  • type logical, which classifies the values true and false
  • type number, which classifies number values
  • type time, which classifies time values
  • type date, which classifies date values
  • type datetime, which classifies datetime values
  • type datetimezone, which classifies datetimezone values
  • type duration, which classifies duration values
  • type text, which classifies text values
  • type binary, which classifies binary values
  • type type, which classifies type values.
  • type list, which classifies list values
  • type record, which classifies record values
  • type table, which classifies table values
  • type function, which classifies function values
  • type anynonnull, which classifies all values excluding null

由于 Type 没有得到严格执行 (MS type doc),并且缺少来自 Microsoft 的任何 'style guide',这使得这个问题成为一个见仁见智的问题。我会使用原始类型(如 M 语言规范第 5 章中所述),除非您明确需要像 Int16.Type 这样的非原始类型作为您的解决方案。