3 ms·
In a functional language like Haskell or O'Caml, it feels like half of what you write involves enums (sum types). They're fantastic in those contexts and I real
by bo1024 5y ago
In a functional language like Haskell or O'Caml, it feels like half of what you write involves enums (sum types). They're fantastic in those contexts and I really miss them in python. But OO is a different approach to data and I suspect the "code smell" advice comes from not wanting to mix the two paradigms. For example
Functional:
data Switch = On | Off
function flip: Switch -> Switch
flip s = case s of
On -> Off
Off -> On
Object Oriented:
abstract class Switch { Switch flip(Switch); }
class On : Switch { flip(s) { return new Off(); }
class Off : Switch { flip(s) { return new On(); }
Weird mixture:
enum State {On, Off};
class Switch {
State myState;
Switch flip(Switch s) {
if (s.myState == Off) { s.myState = On; return s ; }
if (s.myState == On) { s.myState = Off; return s; }
- shikoba 5y agoWhere is File_not_found?