个性化阅读
专注于IT技术分析

F#泛型

点击下载

F#允许你编写通用函数, 方法, 类型, 变量等。它有助于避免每种类型的代码重复。通过编写通用代码, 你可以将其应用于任何类型或值。

句法:

// Generic function.
let function-name<type-parameters> parameter-list =
 function-body

// Generic method.
[ static ] member object-identifer.method-name<type-parameters> parameter-list [ return-type ] =
method-body

// Generic class, record, interface, structure, // or discriminated union.
type type-name<type-parameters> type-definition

F#通用函数示例

let genericFunctionExample<'T> x y =
    printfn "%A %A" x y

genericFunctionExample<int> 1 2

输出:

1 2
赞(0)
未经允许不得转载:srcmini » F#泛型

评论 抢沙发

评论前必须登录!