F# Training
F# Training
F# Training
  • Presentation
  • Fundamentals
    • Introduction
    • Syntax
      • Bases
      • Functions
      • Rules
      • Exceptions
    • First concepts
    • šŸ”Quiz
  • Functions
    • Signature
    • Concepts
    • Syntax
    • Standard functions
    • Operators
    • Addendum
    • šŸ”Quiz
    • šŸ“œSummary
  • Types
    • Overview
    • Tuples
    • Records
    • Unions
    • Enums
    • Anonymous records
    • Value types
    • šŸ“œRecap
    • Addendum
  • Monadic types
    • Intro
    • Option type
    • Result type
    • Smart constructor
    • šŸš€Computation expression (CE)
    • šŸš€CE theoretical basements
    • šŸ“œRecap
  • Pattern matching
    • Patterns
    • Match expression
    • Active patterns
    • šŸš€Fold function
    • šŸ“œRecap
    • šŸ•¹ļøExercises
  • Collections
    • Overview
    • Types
    • Common functions
    • Dedicated functions
    • šŸ”Quiz
    • šŸ“œRecap
  • Asynchronous programming
    • Asynchronous workflow
    • Interop with .NET TPL
    • šŸ“œRecap
  • Module and Namespace
    • Overview
    • Namespace
    • Module
    • šŸ”Quiz
    • šŸ“œRecap
  • Object-oriented
    • Introduction
    • Members
    • Type extensions
    • Class, Struct
    • Interface
    • Object expression
    • Recommendations
Powered by GitBook
On this page
  • Syntax
  • Content
  • Scope
  • šŸš€ Recursive namespace

Was this helpful?

Edit on GitHub
  1. Module and Namespace

Namespace

Syntax

namespace [rec] [parent.]identifier

  • rec for recursive → see slide next

  • parent for grouping namespaces

Content

A namespace F♯ can only contain local types and modules

  • Cannot contain values or functions ā—

  • By comparison, it's the same in C♯ with namespace that contains classes / enums only

What about nested namespaces?

  • Only happens declaratively namespace [parent.]identifier

  • 2 namespaces declared in the same file = not nested but independent

Scope

  • Several files can share the same namespace

  • Several namespaces can be declared in a single file

    • They will not be nested

    • May cause confusion ā—

ā˜ Recommendations \

  • Only one namespace per file, declared at the top

  • Organize files by grouping them in directories with the same name as the namespace

Level
Folder
Namespace

0 (root)

/

Root

1

/Lev1

Root.Lev1

2

/Lev1/Lev2

Root.Lev1.Lev2

šŸš€ Recursive namespace

Extends the default unidirectional visibility: from bottom to top → each element can see all the elements in a recursive namespace

namespace rec Fruit

type Banana = { Peeled: bool }
    member this.Peel() =
        BananaHelper.peel  // `peel` not visible here without the `rec`

module BananaHelper =
    let peel banana = { banana with Peeled = true }

āš ļø Drawbacks: slow compilation, risk of circular reference

ā˜ Recommendation: handy but only for very few use cases

PreviousOverviewNextModule

Last updated 1 month ago

Was this helpful?