Decoupling from dependencies

I wrote a new blog post.

Just a simple idea but hopefully worth sharing

8 Likes

Interesting article!

I wonder: why did you choose data over typeclasses?

This

seems exactly what a typeclass is suited for, am I wrong?

You’re not wrong at all. Typeclasses are another (probably more standard) way to declare an interface in Haskell.

I actually plan to write a follow-up post on several way to declare an interface in Haskell.

This post by Gabriella Gonzales details some reasons why sometime a typeclass could not be the best tool for declaring an interface

1 Like

I have problems checking how applicationBar works.

applicationBar :: OurOwnFoo -> String
applicationBar = ourOwnBarImplementation.ourOwnBar

ourOwnBar has type OurOwnFoo -> String, which is fine. But then ourOwnBarImplementation has type OurOwnBar. What am I missing?

I think I used too many similar types…

ourOwnBarImplementation has type OurOwnBar. OurOwnBar has a field ourOwnBar of type OurOwnFoo -> String.

Hence ourOwnBarImplementation.ourOwnBar should have type OurOwnFoo -> String, unless I’m missing something

data Foo = Foo
data OurOwnFoo = OurOwnFoo
data OurOwnBar = OurOwnBar {ourOwnBar :: OurOwnFoo -> String}

bar :: Foo -> String
bar = undefined

encodeFoo :: OurOwnFoo -> Foo
encodeFoo = undefined

ourOwnBarImplementation :: OurOwnBar
ourOwnBarImplementation = OurOwnBar $ bar . encodeFoo

applicationBar :: OurOwnFoo -> String
applicationBar = ourOwnBarImplementation.ourOwnBar

errors with:

/tmp/prova.hs:15:18-40: error: [GHC-83865]
    • Couldn't match expected type ‘(OurOwnFoo -> String) -> String’
                  with actual type ‘OurOwnBar’
    • In the first argument of ‘(.)’, namely ‘ourOwnBarImplementation’
      In the expression: ourOwnBarImplementation . ourOwnBar
      In an equation for ‘applicationBar’:
          applicationBar = ourOwnBarImplementation . ourOwnBar
   |
15 | applicationBar = ourOwnBarImplementation.ourOwnBar
   |                  ^^^^^^^^^^^^^^^^^^^^^^^
/tmp/prova.hs:15:42-50: error: [GHC-83865]
    • Couldn't match type ‘OurOwnBar’ with ‘OurOwnFoo’
      Expected: OurOwnFoo -> OurOwnFoo -> String
        Actual: OurOwnBar -> OurOwnFoo -> String
    • In the second argument of ‘(.)’, namely ‘ourOwnBar’
      In the expression: ourOwnBarImplementation . ourOwnBar
      In an equation for ‘applicationBar’:
          applicationBar = ourOwnBarImplementation . ourOwnBar
   |
15 | applicationBar = ourOwnBarImplementation.ourOwnBar

Ah, I forgot OverloadedRecordDot!

1 Like