Rory A Campbell
Rory A Campbell

Reputation: 131

How to have module consumer define type in Elm

I've created a table module in my Elm project.

Each column has a getter function that defines how they retrieve data from the Records that model the rows.

So a Row could look something like:

{ name = "bananas"
, price = 5
}

and the columns could look like this:

[ { title = "Item Name"
  , get = .name
  }
, { title = "Price in pennies"
  , get = .price >> (*) 100 >> toString
  }
]

This means the type annotation for the column get function is: Row -> String

The problem I have is that I want to make this table module a reusable component for other projects that have their own "Row" type. How do I allow the consumer of the module to specify the Row type alias without removing all my type annotations?

I'm quite new to Elm so sorry if the wording in my question is off.

Upvotes: 0

Views: 70

Answers (1)

bdukes
bdukes

Reputation: 156055

Instead of using your own type, Row, you can introduce a type variable (often a, but any lower-case name will work, e.g. data in elm-sortable-table)

You can then create something like this:

type alias Column a =
    { get : a -> String
    , title : String
    }

type alias Columns a =
    List (Column a)

viewTable : Columns a -> List a -> Html msg

Upvotes: 4

Related Questions