F#模块

F#模块 首页 / F#入门教程 / F#模块

模块是类、函数和类型的集合。它有助于组织相关代码,以便无涯教程可以轻松维护代码。

它被实现为只有Static成员的公共语言运行库(Clr)类。

语法:

//Top-level module declaration.
module [accessibility-modifier] [qualified-namespace.]module-name
	declarations
//Local module declaration.
module [accessibility-modifier] module-name =
    declarations

如果不在代码中指定或声明模块,则会隐式创建一个与文件名相同但不带扩展名的模块。档案的第一个字母转换为大写字母。

无涯教程网

F#模块示例1

module Arithmetic = 
 let add a b =
  a+b
 let sub a b =
  a-b
 let mul a b = 
  a*b
 let div a b =
  a/b

您可以使用两种不同的方式访问模算术的所有方法。

  1. Fully qualify the function name
  2. Open the module

F#模块示例2

printf "%d" (Arithmetic.add 10 20)

输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/fsharp/f-sharp-modules.html

来源:LearnFk无涯教程网

30

F#模块示例3

open Arithmetic
printf "%d" (sub 20 10)

输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/fsharp/f-sharp-modules.html

来源:LearnFk无涯教程网

10

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

教程推荐

大型Android系统重构实战 -〔黄俊彬〕

云原生架构与GitOps实战 -〔王炜〕

如何读懂一首诗 -〔王天博〕

深度学习推荐系统实战 -〔王喆〕

软件设计之美 -〔郑晔〕

系统性能调优必知必会 -〔陶辉〕

NLP实战高手课 -〔王然〕

Linux实战技能100讲 -〔尹会生〕

玩转Spring全家桶 -〔丁雪丰〕

好记忆不如烂笔头。留下您的足迹吧 :)