Suppose, for example, you want to implement a spreadsheet Cell in C++. A cell can be either a string, a number, or perhaps empty. Ignore other cases, like it being a formula.
In Haskell, you might do something like:
data Cell = CellStr String | CellDbl Double | None
What is considered the current "best practice" for doing it in C++? Use a union in a structure with a type indicator, or something else?
boost::variant
. – Estivationvector<pair<XYCoords, double>> doubles;
and a sortedvector<pair<XYCoords, string>> strings;
. For a given cell coordinate youlower_bound
into thedoubles
, if you didn't find it you do the same for thestrings
, otherwise it isNone
. Drawing the screen should be very fast, you just iterate through thevector
s. Calculations are a bit messy, because they depend on the type, but you can probably abstract that away. Effectively I just cheated and never combined different types into one. Anyway, the question is too broad and opinionated. – Vetavetchlang-hs
instead oflang-haskell
. Keep this in mind the next time you want to add highlighting of Haskell code. – Outermost