[Haskell-beginners] Haskell way of defining and implementing OO interfaces

Julian Birch julian.birch at gmail.com
Sun Jan 4 13:00:47 UTC 2015


Consider that

interface PasswordStore {
  void store(Path path, String secret, Map metadata);
}

is identical to

void store (PasswordStore store, Path path, String secret, Map metadata)

or

store :: PasswordStore -> Path -> secret -> MetaData -> IO ()

So, you can treat PasswordStore as a pure data structure (that has things
like connection details) and just define functions that use it.  I wouldn't
worry about grouping the functions together.(*) I'm going to assume you
don't really need an actual interface, but if you did, you could
investigate typeclasses.

Julian.

(*) In general terms, the only reason to group functions together is to
enforce laws that relate the behaviours together e.g. that you can retrieve
something you stored.

On 4 January 2015 at 11:14, Thomas Koch <thomas at koch.ro> wrote:

> Hi,
>
> I'm writing a password manager that implements a dbus-api using the dbus[1]
> package. I'd like to separate the code that implements from the dbus api
> from
> the code that stores and retrieves the secrets (passwords). In Java I'd
> use an
> interface, e.g.:
>
> interface PasswordStore {
>   void store(Path path, String secret, Map metadata);
>   (String secret, Map metadata) retrieve(Path path);
>   (String secret, Map metadata) search(Map criteria);
> }
>
> And the dbus-api would export this interface:
>
> dbusClient.export(PasswordStore store)
>
> What would be a Haskell way to do the same? My only idea is to define a
> record:
>
> data PasswordStore {
>     store :: Path -> Secret -> MetaData -> IO ()
>   , retrieve :: Path -> IO (Secret, MetaData)
>   , search :: Criteria -> IO (Secret, MetaData)
> }
>
> Thank you for any suggestions! Thomas Koch
>
> [1] http://hackage.haskell.org/package/dbus-0.10.9
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20150104/63901ded/attachment.html>


More information about the Beginners mailing list