是否有创建 table 类型的 M 函数?

Is there an M function to create a table type?

M语言中有Type.ForRecord函数可以动态创建记录类型

// These two expressios are exchangeable

type [A = Int64.Type, optional B = text]

Type.ForRecord(
    [
         A = [Type = type number, Optional = false],
         B = [Type = type text, Optional = true]
    ],
    false
)

使用它,我们能够基于现有类型创建一个新的记录类型,并添加如下所示的附加字段。

// These two expressions are exchangeable

type [A = Int64.Type, optional B = text, optional C = date]

let
    existingType = [A = Int64.Type, optional B = text],
    newFieldName = "C",
    newFieldType = type date
in
    Type.ForRecord(
        Record.AddField(
            Type.RecordFields(existingType),
            newFieldName,
            [Type = newFieldType, Optional = true]
        ),
        false
    )

现在,我正在寻找一种方法来对 table 类型执行类似的操作。我希望能够修改现有的 table 类型并添加一个新列。新列的名称和类型在运行时动态确定。

// I want this result

type table [A = Int64.Type, B = text, C = date]

// How can I create a new table type adding column `C = date`?

let
    existingType = type table [A = Int64.Type, B = text],
    newColumnName = "C",
    newColumnType = type date
in
    ...

我期待有一个类似 Type.ForTable 的函数,但我找不到这样的函数。有什么想法吗?

执行此操作的简单方法如下:

newType = type table [A = Int64.Type, B = text, newFieldName = newFieldType]

但如果您的 existingType 是动态的,那将行不通。


如果您将 existingType 定义保留为通用记录(而不是 table),则可以通过将其转换为 [=21= 来获取新的 table 类型] 添加新记录字段后键入。

let
    existingType = type /*no table here*/ [A = Int64.Type, B = text],
    newFieldName = "C",
    newFieldType = type date,
    newType =
    Type.ForRecord(
        Record.AddField(
            Type.RecordFields(existingType),
            newFieldName,
            [Type = newFieldType, Optional = false]
        ),
        false
    ),
    newTableType = type table newType
in
    newTableType