Getting Started With Access Specifier In F#

Introduction

This article explains access specifiers in F#. My intent is for you to become familiar with the basic access specifiers used in the F# programming language. Access specifiers in F# are part of Object Oriented Programming languages that allow classes to restrict the accessebility level in a program.

What is Access Specifier in F#?

Access specifiers are keywords in F# that allow us to restrict the availability of an object, method, class and its members in the program or in an application. We can control the scope of the member object of a class using access specifiers that provide security of applications.

Types Of Access Specifiers

The following are the types of Access Specifiers in F#:

  • Public

  • Private

  • internal

Public

The Public access specifier is the most commonly used modifier. Any member, class, field and method that specify the public access specifier then it can be accessed from anywhere. In other words there is no restriction on their accessibility. The scope the variables, classes and methods are inside the classes as well as we can call these from outside the scope of the class. The type or member can be accessed by any other code in the same assembly or another assembly that references it.

Scope
 
The scope of the Public Modifier is:
  • Can be accessed from anywhere inside the class or outside the class.
  • Can be accessed by an object of the class.
  • Can be accessed by derived classes.
  • Outside the class and outside the assembly.

Private

The type or member can be accessed only by code in the same class. The scope of the private members extend only to inside the class where it has been declared; they cannot be accessed from outside the class. So, the Private access specifier is the opposite of the Public access specifier. It is mostly used for encapsulation: data is hidden within the class and accessor methods are provided.

Scope
 
The scope of the Private Modifier is:
  • Can be accessed only within the class in which it is declared.

  • Private methods and members of a class cannot be accessed by subclasses.

Internal

The members that are declared as Internal can only be accessed "within the assembly (DLL or exe)".

Scope
 
The scope of the Internal Modifier is:
  • Within the class in which they are declared. 

  • Within the derived classes of that class available within the same assembly.

  • Outside the class within the same assembly.

How to differentiate between Private and Internal modifiers in programs let's see the example given below.

Step 1:

There are two file in the project, "module1.fs" and "module2.fs." There are two modules, "module1" and "module2". Private type and internal type modifiers are defined in module1 that are in the module1.fs file. The Private type cannot be accessed from module2 but we can accss an internal type.

Step 2:

Write the following code in module1 (module1.fs file):

 module module1

open System

typeprivate MethodExample() =

   

    // standalone method

    member this.AddOne x =

        x + 1

 

    // calls another method

    member this.AddTwo x =

        this.AddOne x |> this.AddOne

 

typeinternal internalExample()=

    // parameterless method

    member this.Pi() =

        3.14159

 

// test

letprivate privateobj = new MethodExample()

letinternal internalobj=new internalExample()

printfn "%i" <| privateobj.AddOne 45

printfn "%i" <| privateobj.AddTwo 42

printfn "%f" <| internalobj.Pi()

System.Console.ReadKey(true)

 

Step 3:

Debug the program by pressing F5; the output will appear on the command prompt.

outputmodule1.jpg

Step 4:

Now add one file, module2.fs, and put the following code in this file.

// Module2.fs

module Module2

open module1

open System

letinternal myInternalObj = new internalExample()

let result = myInternalObj.Pi

printfn "%f" <| myInternalObj.Pi()

System.Console.ReadKey(true)

 

 Step 5:

Debug the file (module2.fs) by pressing F5. You will get a result in the command prompt; let's see the figure.

 

outputmodule2.jpg

 


Similar Articles