Over a million developers have joined DZone.

Closure-based State in F#

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

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

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 =
        abstract Operation: (int) -> int

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

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

Alternatively, F# can return an instance of a Dynamic Object; 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.

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

f# ,closures

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

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}