I am trying to create a solution with a library of a lower level that will know that it needs to save and load data when certain commands are called, but the implementation of the save and load functions will be provided on a platform-specific project that references the lower level library.
I have some models, for example:
type User = { UserID: UserID Situations: SituationID list } type Situation = { SituationID: SituationID }
And what I want to do is the ability to define and call functions such as:
do saveUser () let user = loadUser (UserID 57)
Is there a way to define this purely in a functional idiom, preferably avoiding a volatile state (which should not be necessary in any case)?
One way to do this might look something like this:
type IStorage = { saveUser: User->unit; loadUser: UserID->User } module Storage = // initialize save/load functions to "not yet implemented" let mutable storage = { saveUser = failwith "nyi"; loadUser = failwith "nyi" } // ....elsewhere: do Storage.storage = { a real implementation of IStorage } do Storage.storage.saveUser () let user = Storage.storage.loadUser (UserID 57)
And there are variations in this, but everything that I can think of is connected with some kind of uninitialized state. (Xamarin also has a DependencyService, but this is a dependency itself, which I would like to avoid.)
Is there a way to write code that calls a storage function that is not yet implemented, and then implement it WITHOUT using a mutable state?
(Note: this question does not concern the repository itself, but only what I use. It's about how to enter functions without using an unnecessary mutable state.)
functional-programming abstraction f # xamarin storage
Overlord zurg
source share