Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9126085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:57:21+00:00 2026-06-17T06:57:21+00:00

What is the idiomatic Haskell solution for dependency injection? E.g., suppose you have an

  • 0

What is the idiomatic Haskell solution for dependency injection?

E.g., suppose you have an interface frobby, and you needed to pass an instance conforming to frobby around (there might be multiple varieties of these instances, say, foo, and bar).

Typical operations would be:

  • functions that take some value X and return some value Y. E.g., this might be a database accessor, taking a SQL query & a connector and returning a dataset. You might need to implement postgres, mysql, and a mock test system.

  • functions that take some value Z and return a closure relating to Z, specialized to a given foo or bar style, chosen at runtime.

One person solved the problem as follows:

http://mikehadlow.blogspot.com/2011/05/dependency-injection-haskell-style.html

But I don’t know if that’s the canonical way to manage this task.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:57:22+00:00Added an answer on June 17, 2026 at 6:57 am

    I think the proper answer here is, and I will probably receive a few downvotes just for saying this: forget the term dependency injection. Just forget it. It’s a trendy buzzword from the OO world, but nothing more.

    Let’s solve the real problem. Keep in mind that you are solving a problem, and that problem is the particular programming task at hand. Don’t make your problem “implementing dependency injection”.

    We’ll take the example of a logger, because that’s a basic piece of functionality many programs will want to have, and there are lots of different types of loggers: One that logs to stderr, one that logs to a file, a database, and one that simply does nothing. To unify all them you want a type:

    type Logger m = String -> m ()
    

    You could also choose a fancier type to save some keystrokes:

    class PrettyPrint a where
        pretty :: a -> String
    
    type Logger m = forall a. (PrettyPrint a) => a -> m ()
    

    Now let’s define a few loggers using the latter variant:

    noLogger :: (Monad m) => Logger m
    noLogger _ = return ()
    
    stderrLogger :: (MonadIO m) => Logger m
    stderrLogger x = liftIO . hPutStrLn stderr $ pretty x
    
    fileLogger :: (MonadIO m) => FilePath -> Logger m
    fileLogger logF x =
        liftIO . withFile logF AppendMode $ \h ->
            hPutStrLn h (pretty x)
    
    acidLogger :: (MonadIO m) => AcidState MyDB -> Logger m
    acidLogger db x = update' db . AddLogLine $ pretty x
    

    You can see how this builds a graph of dependencies. The acidLogger depends on a database connection for the MyDB database layout. Passing arguments to functions is about the most natural way to express dependencies in a program. After all a function is just a value that depends on another value. That is also true for actions. If your action depends on a logger, then naturally it is a function of loggers:

    printFile :: (MonadIO m) => Logger m -> FilePath -> m ()
    printFile log fp = do
        log ("Printing file: " ++ fp)
        liftIO (readFile fp >>= putStr)
        log "Done printing."
    

    See how easy this is? At some point this makes you realize how much easier your life will be, when you just forget all the nonsense that OO has taught you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

HLint is a Haskell lint tool for making code more idiomatic. Is there something
Is there an idiomatic Scala solution to obtain only the file names from File.listFiles
If I have a List[Option[A]] in Scala, what is the idiomatic way to filter
I'm writing a game in Haskell, and my current pass at the UI involves
What is the (or is there an) idiomatic way to strip newlines from strings
I'm just learning Haskell, and trying to figure out the most idiomatic way to
Is there an idiomatic way of doing this?
Is there a more concise and idiomatic way to write the following code, which
Can you guys think of the shortest and the most idiomatic solution to all-but-one
I have a dead simple Common Lisp question: what is the idiomatic way of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.