F # Learning Road (3) How to organize the program

Source: Internet
Author: User

Modules (module)

In F #, modules are typically used to encapsulate a set of variables and operations on a data structure, such as List,map,seq in F #, which are used to manipulate data structures such as list lists, Map key value pairs, and seq sequences.

1. Define a module

Define a module in F # using the keyword module. If you do not explicitly define a module in a source code file (. fs file), F # defaults you define a module with a source file name, whose scope is from the beginning of the file to the end of the file, as described in the previous article.

Code

(文件1:a.fs)
#light
module A
let l=[1;2;3]
(文件2:b.fs)
module B
print_any A.l
System.Console.ReadKey(true)

File 1 defines a module A, defines a list L, file 2 defines a module B, and uses the Print_any function to print the L-List of module B.

The list in F # uses square brackets, separated by semicolons between elements, and is immutable once defined

There are two types of modules in F #, one is the top-level module and the other is a child module. The so-called top-level module refers to itself not nested in another module, and one nested in another module is called a child module. The two modules are defined in a different way, and the child modules are described in the following namespaces.

There are two ways to define a top-level module, in addition to the method defined in the previous code one, there is a

Module A.B.C

This form of dotted-number separation, which represents the definition of a namespace a.b under the current namespace (namespace), and the definition of module C under A.B

The definition of a top-level module requires the first sentence in addition to the definition or instruction of a namespace.

Modules in F # are equivalent to static sealed classes in C #. You can use reflector to see that the types, values, and functions defined in the module become static members of this static class.

Code

(文件3:stringExtension.fs)
#light
module Util.StringExtensions
let length (s:string) = s.Length
type System.String with
member x.Link =
"<a>"+x+"</a>"
System.Console.ReadKey(true)
(文件4:test.fs)
#light
open Util.StringExtensions
let s="Test".Link
let len=length s
printfn "%s" s
printfn "%d" len
System.Console.ReadKey(true)

In code file 3 above, a module with a namespace is defined, and in code file 4, the length function defined by the module in 3 is called and the extension string is a property of the member link

The Open keyword is used here to explicitly require the compiler to look for values and types, functions, and so on under Util.stringextensions. This allows us to not use the fully qualified name. Open in F # not only can open a namespace, you can also open a module. As I said before, modules are equivalent to static classes (I haven't found a way to define static classes explicitly in F #), so the module is an ideal place to put the human-scale static tool class in OO.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.