O'Reilly logo

Programming F# by Chris Smith

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Modules

If you recall from Chapter 2, namespaces are a way to organize types. Modules behave much like namespaces, except that instead of being able to contain only types, modules can contain values as well.

While modules’ ability to contain values makes it easy to write simple applications, they are not well suited for being shared with other programmers and .NET languages. A well-designed class can stand on its own, and is simple and intuitive to use because all of its complex inner workings are hidden. With a module, you are given a loose collection of values, functions, and types, and it can be unclear how they all relate.

Identifying when and how to convert a module into a namespace and class is important for mastering functional programming in F#.

Converting Modules to Classes

Simply put, code defined in modules doesn’t scale as well as typical object-oriented hierarchies. Ten modules with dozens of values and types is manageable, but hundreds of modules with thousands of values and types is not. Eliminating the loose values and just relying on classes is one way to help combat complexity.

The code in Example 7-12 creates a simple screen scraper, or a program that downloads the images at a particular URL. You can see how code like this can easily grow out of a long FSI session: typing some code in the FSI window until it works and then copying it to the code editor later. While the code works in the context of a single code file and an FSI session, it cannot easily be integrated ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required