Cleanest design for having same behaviour of function(s) working on (slightly) different types?

beginners

#1

I have a function that has signature

(string * TaggedExpense list) list    -> CategorisedExpenseSummary list

with the type definition type CategorisedExpenseSummary = string*ExpensesSummary.

Now I need the exact same transformation done, but with the types

(datetime * TaggedExpense list) list    -> CategorisedExpenseSummary list

and type DateExpenseSummary = datetime*ExpensesSummary.

What’s the cleanest design in this case?


#2

Depending on your use-case, you can choose between SRTPs and generics.


#3

It almost feels like you want a parameterised ExpenseSummary<'T>, where the first element of the tuple could then be string/datetime/etc. Then your overall transformation would be ('T * TaggedExpense list) list -> 'T ExpenseSummary and you’re good to go. Maybe make a type alias for that to make it easier to read.