Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pluggability in a functional paradigm

What is the proper functional way to handle pluggability in projects? I am working on a new opensource project in F# and can not seem to get the object oriented idea of plugins and interfaces out of my mind. Things I would like to be able to swap out are loggers, datastoring, and authentication.

I have been searching quite a bit for an answer to this and havent come up with much except for this: http://flyingfrogblog.blogspot.com/2010/12/extensibility-in-functional-programming.html

like image 576
Tyler Smith Avatar asked Jun 16 '26 07:06

Tyler Smith


1 Answers

The answer to this question would be different for different functional languages. F# is not purely functional - it takes the best from functional, imperative and also object-oriented worlds.

For things like logging and authentication, the most pragmatic approach would be to use interfaces (in F#, it is perfectly fine to use interfaces, but people do not generally use inheritance and prefer composition instead).

A simple interface makes sense when you have multiple different functions that you can invoke:

 type IAuthentication =
   abstract Authenticate : string * string -> bool
   abstract ResetPassword : string * string -> void

You can use object expressions, which is a really nice way to implement interfaces in F#. If you have just a single function (like logging a message), then you can parameterize your code by a function (which is like an interface with just a single method):

 type Logger = string -> unit

For things like authentication and logging (that probably do not change while the application is running), you can use a global mutable value. Although if you want to synchronize requests from multiple threads and there is some mutable state, it might be a good idea to write an F# agent.

like image 111
Tomas Petricek Avatar answered Jun 18 '26 00:06

Tomas Petricek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!