DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • In-Depth Guide to Using useMemo() Hook in React
  • Revolutionizing Network Operations With Automated Solutions: A Deep Dive Into ReactJS
  • The ABCs of Unity's Coroutines: From Basics to Implementation
  • 4 Email Validation and Verification Techniques for App Developers

Trending

  • Ujorm3: A New Lightweight ORM for JavaBeans and Records
  • A Hands-On ABAP RESTful Programming Model Guide
  • How to Write for DZone Publications: Trend Reports and Refcards
  • Master-Class: Understanding Database Replication (Single, Multi, and Leaderless)
  1. DZone
  2. Coding
  3. Languages
  4. Closure-based State in F#

Closure-based State in F#

How to use F# to store state in object or class fields.

By 
Ted Neward user avatar
Ted Neward
·
Apr. 29, 16 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
12.4K Views

Join the DZone community and get the full member experience.

Join For Free

Being an object/functional hybrid language, F# offers the opportunity to support either a traditional object-field approach to encapsulating state, as well as the more “functional” style of Closure-based State. As such, developers will generally prefer to store state in object/class fields, but there can still be situations where encapsulating the state away from the object as a whole can be preferable.

However, F# has some interesting language restrictions around enclosed bound variables; specifically, because F# wants to assume that all bound values are immutable by default, F# will require a slight amendment to the enclosed local state, marking it as a “reference” (using the ref keyword), before it can be used inside of a returned function:

let operation =
    let state = ref 100
    fun adjust ->
        state := (!state) + adjust

Thanks to F#’s “last expression in an expression block is the assumed return value” feature, making use of this feels absolutely trivial; the anonymous function receiving the single parameter adjust is assigned to the local value operation, meaning that now operation will be operating on the referenced state value, but without any sort of opportunity for any other operation or library to modify state.

When working with objects this gets trickier, since F# (like its kin C# and Visual Basic) is a strongly-typed language, and as such wants clients to have compile-time awareness of the object types they are invoking. Thus, the typical idiomatic usage for F# here will be to use Closure-based State to implement an anonymous implementation of an interface, and have the outer function declared to return an instance of such. This is trivial to do with object expressions:

type IInterface =
    interface
        abstract Operation: (int) -> int
    end

let instance =
    let state = ref 100
    { new IInterface with
        member x.Operation(adjust) = 
            state := (!state) + adjust
            !state
    }

let result = instance.Operation(100)
printfn "%A" result

Alternatively, F# can return an instance of a ; this is somewhat contradictory to the spirit of the F# language, since it is strongly-typed by nature, but circumstances will sometimes suggest it as useful, depending (as always) on the context.

Object (computer science) dev Blocks Library Sort (Unix) Interface (computing) Implementation

Published at DZone with permission of Ted Neward. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • In-Depth Guide to Using useMemo() Hook in React
  • Revolutionizing Network Operations With Automated Solutions: A Deep Dive Into ReactJS
  • The ABCs of Unity's Coroutines: From Basics to Implementation
  • 4 Email Validation and Verification Techniques for App Developers

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook